<%page args="module"/>
<%namespace name="lib" file="//lib.html" import="*"/>
<%!
from splunk.appserver.mrsparkle.lib import viewstate, message
from splunk import entity
import cherrypy
import json
import splunk
%>
<%
proxyParams = {}

# try to get the saved search object
savedSearch = module['params'].get('savedSearch')
if not savedSearch and DATA['mode'] == 'saved':
    savedSearch = DATA['name']


# check saved search for 
vsid = None
displayview = None
if savedSearch:
    try:
        obj = entity.getEntity('saved/searches', savedSearch, namespace=APP['id'])
        vsid = obj.get('vsid')
        displayview = obj.get('displayview') or '*'
    except splunk.ResourceNotFound:
        if VIEW['nativeObjectMode']!='SimpleDashboard':
            message.send_client_message('warn', _('Cannot find the saved search %s') % savedSearch)

elif DATA['mode'] == 'sid' and DATA.get('vsid'):
    vsid = DATA.get('vsid')
    displayview = '*'
    

# if a viewstate is found, inject into params
if vsid:
    vsObject = None
    try:
        vsObject = viewstate.get(displayview, vsid, APP['id'], cherrypy.session['user'].get('name'))
    except splunk.ResourceNotFound:
        message.send_client_message('warn', _('Unable to get viewstate information; formatting may not be correct'))

    if vsObject:
        for moduleId in vsObject.modules:
            straightModule = moduleId.split('_', 1)[0]
            proxyParams[straightModule] = vsObject.modules[moduleId]


%>
% if proxyParams:
    <%call expr="lib.add_script_block()">
        Splunk.Module.loadParams.${module['id']}.proxyParams = ${json_decode(proxyParams)};
    </%call>
% endif
