import{R as e,F as t}from"./p-e6fe58b7.js";import{D as i,d as s}from"./p-b2212d20.js";import{F as r}from"./p-ff1990ad.js";import{ObjectUtils as a}from"@sankhyalabs/core";var n;function l(e,t){if(null==e)return e;if(e instanceof Date)return e.toISOString();if("object"==typeof e){if(e instanceof Array)return e.map((e=>l(r.MULTI_LIST===t?function(e,t){var i={};for(var s in e)Object.prototype.hasOwnProperty.call(e,s)&&t.indexOf(s)<0&&(i[s]=e[s]);if(null!=e&&"function"==typeof Object.getOwnPropertySymbols){var r=0;for(s=Object.getOwnPropertySymbols(e);r<s.length;r++)t.indexOf(s[r])<0&&Object.prototype.propertyIsEnumerable.call(e,s[r])&&(i[s[r]]=e[s[r]])}return i}(e,["label"]):e)));{const i=Object.assign({},e);return Object.keys(e).forEach((s=>{void 0!==e[s]?i[s]=l(e[s],t):delete i[s]})),i}}return e}class o{constructor(){this.DEFAULT_RESOURCE="_default",this.templateByQuery=new Map,this.buildTemplates()}resolveURI(e,t,i){let s="filter-assistent-config://"+e;return t&&(s+="."+t),i&&(s+=this.DEFAULT_RESOURCE),s}resolveLegacyConfigName(e,t){if(!e)return"";let i=`?legacyResourceID=${e}`;return t&&(i+=this.DEFAULT_RESOURCE),i}buildTemplates(){this.templateByQuery.set("fetchFilterFields",s.gql`
    query($name: String!) {
      $queryAlias$: fetchFilterFields(name: $name){
        currentLink {
          description
          uri
        }
        links {
          description
          uri
          fieldCount
        }
        fields {
          name
          description
          uri
          onlyDate
          dataType
          userInterface
          entityName
          properties {
            name
            value
          }
        }
      }
    }
    `),this.templateByQuery.set("fetchFilterAssistent",s.gql`
    query($name: String!) {
      $queryAlias$: fetchFilterAssistent(name: $name) {
        id
        name
        active
        expression
        warningMessage
        assistent
      }
    }`),this.templateByQuery.set("savePersonalizedFilter",s.gql`
    mutation($name: String!, $filterAssistent: InputFilterAssistent!)  {
      $queryAlias$: savePersonalizedFilter(name: $name, filterAssistent: $filterAssistent) {
        id
        name
      }
    }`),this.templateByQuery.set("deletePersonalizedFilter",s.gql`
    mutation($name: String!, $filterId: String!) {
      $queryAlias$: deletePersonalizedFilter(name: $name, filterId: $filterId) {
        id
        name
      }
    }`),this.templateByQuery.set("validateExpression",s.gql`
    query($dataUnitName: String!, $expression: String!) {
      $queryAlias$: validateExpression(dataUnitName: $dataUnitName, expression: $expression) {
        valid
        message
      }
    }`)}async loadDefaultFilter(e,t,i,s){return this.loadPersonalizedFilter(e,t,i,s,!0)}async loadPersonalizedFilter(e,t,s,r,a){return new Promise(((n,l)=>{i.get().callGraphQL({values:{name:this.resolveURI(e,s,a)+"/"+t+this.resolveLegacyConfigName(r,a)},query:this.templateByQuery.get("fetchFilterAssistent")}).then((e=>{e.assistent=JSON.parse(e.assistent),n(e)})).catch((e=>{l(e)}))}))}async saveDefaultFilter(e,t,i){return this.savePersonalizedFilter(e,t,i,!0)}async savePersonalizedFilter(e,t,s,r){if(!e||0==Object.keys(e).length)return Promise.reject(new Error("personalizedFilter não pode ser vazio"));let a=JSON.parse(JSON.stringify(e));return a.assistent=JSON.stringify(a.assistent),new Promise(((e,n)=>{i.get().callGraphQL({values:{name:this.resolveURI(t,s,r),filterAssistent:a},query:this.templateByQuery.get("savePersonalizedFilter")}).then((t=>{e(t)})).catch((e=>{n(e)}))}))}removeDefaultFilter(e,t,i){return this.removePersonalizedFilter(e,t,i,!0)}async removePersonalizedFilter(e,t,s,r){return new Promise(((a,n)=>{i.get().callGraphQL({values:{name:this.resolveURI(t,s,r),filterId:e.id},query:this.templateByQuery.get("deletePersonalizedFilter")}).then((e=>{a(e)})).catch((e=>{n(e)}))}))}async validatePersonalizedFilter(e,t){return new Promise(((s,r)=>{i.get().callGraphQL({values:{dataUnitName:e,expression:t},query:this.templateByQuery.get("validateExpression")}).then((e=>{s(e)})).catch((e=>{r(e)}))}))}loadFields(e){return new Promise(((t,s)=>{i.get().callGraphQL({values:{name:e},query:this.templateByQuery.get("fetchFilterFields")}).then((e=>{var i;let s=e.fields;for(const e of s){if(Array.isArray(e.properties)&&e.properties.length>0){const t=e.properties.reduce(((e,t)=>(e[t.name]=t.value,e)),{});e.properties=t}(null===(i=null==e?void 0:e.properties)||void 0===i?void 0:i.options)&&(e.properties.options=JSON.parse(e.properties.options))}t(Object.assign(Object.assign({},e),{fields:s}))})).catch((e=>{s(e)}))}))}}!function(e){e.form="form",e.grid="grid",e.filterBar="filterBar"}(n||(n={}));class c{static addFilterBarLegacyConfig(e,t){this.validateResourceId(t),this.filterBarLegacyConfig.set(e,t)}static addFormLegacyConfig(e,t){this.validateResourceId(t),this.formLegacyConfig.set(e,t)}static addGridLegacyConfig(e,t){this.validateResourceId(t),this.gridLegacyConfig.set(e,t)}static preload(e,t,i){t&&(null==i?void 0:i.gridLegacyConfig)&&c.addGridLegacyConfig(t,i.gridLegacyConfig),t&&(null==i?void 0:i.formLegacyConfig)&&c.addFormLegacyConfig(t,i.formLegacyConfig),c.loadFormConfig(t,e),c.loadGridConfig(t,e)}static async loadFilterBarConfig(e,t,i){i=this.handleLegacyConfig(e,n.filterBar,i);const s=c.buildCacheID(e,n.filterBar,t);return c.configById.has(s)||c.configById.set(s,c.filterBarConfigFetcher.getConfig(t,e,i)),c.configById.get(s)}static async loadFormConfig(e,t){e=this.handleLegacyConfigAsString(e,n.form);const i=c.buildCacheID(e,n.form,t);return c.configById.has(i)||c.configById.set(i,c.formConfigFetcher.loadFormConfig(e,t)),c.configById.get(i)}static async loadGridConfig(e,t){e=this.handleLegacyConfigAsString(e,n.grid);const i=c.buildCacheID(e,n.grid,t);return c.configById.has(i)||c.configById.set(i,c.gridConfigFetcher.getConfig(e,t)),c.configById.get(i)}static async saveFilterBarConfig(e,t,i){return await this.deleteFilterBarConfigCache(t,i),this.filterBarConfigFetcher.saveConfig(e,i,t)}static async saveFormConfig(e,t,i){return t=this.handleLegacyConfigAsString(t,n.form),await this.deleteFormConfigCache(t,i),this.formConfigFetcher.saveConfig(e,t,i)}static async saveCardState(e,t,i){return this.formConfigFetcher.saveCardState(e,t,i)}static async getDefaultGridConfig(e){return await this.gridConfigFetcher.getDefaultConfig(e)}static async saveGridConfig(e,t,i){if(null!=e)return t=this.handleLegacyConfigAsString(t,n.grid),await this.deleteGridConfigCache(t,i),this.gridConfigFetcher.saveConfig(e,t,i)}static async loadDefaultFilter(e,t,i){const s=this.filterBarLegacyConfig.get(i);return this.personalizedFilterFetcher.loadDefaultFilter(t,e,i,s)}static async loadPersonalizedFilter(e,t,i){const s=this.filterBarLegacyConfig.get(i);return this.personalizedFilterFetcher.loadPersonalizedFilter(t,e,i,s)}static async savePersonalizedFilter(e,t,i){return await this.deleteFilterBarConfigCache(i,t),this.personalizedFilterFetcher.savePersonalizedFilter(e,t,i)}static async saveDefaultFilter(e,t,i){return await this.deleteFilterBarConfigCache(i,t),this.personalizedFilterFetcher.saveDefaultFilter(e,t,i)}static async removeDefaultFilter(e,t,i){return this.personalizedFilterFetcher.removeDefaultFilter(e,t,i)}static async removePersonalizedFilter(e,t,i){return this.personalizedFilterFetcher.removePersonalizedFilter(e,t,i)}static async validatePersonalizedFilter(e,t){return this.personalizedFilterFetcher.validatePersonalizedFilter(e,t)}static async deleteGridConfigCache(e,t){await this.deleteConfigCache(e,t,n.grid)}static async deleteFormConfigCache(e,t){await this.deleteConfigCache(e,t,n.form)}static async deleteFilterBarConfigCache(e,t){await this.deleteConfigCache(e,t,n.filterBar)}static validateResourceId(e){if(!e)throw Error(this.LEGACY_CONFIG_NAME_ERROR_MESSAGE)}static handleLegacyConfig(e,t,i){let s;switch(t){case n.filterBar:s=this.filterBarLegacyConfig.get(e);break;case n.grid:s=this.gridLegacyConfig.get(e);break;case n.form:s=this.formLegacyConfig.get(e)}const r=i?Object.assign({},i):{};return s&&(r.legacyResourceID=s),r}static handleLegacyConfigAsString(e,t){const i=this.handleLegacyConfig(e,t).legacyResourceID;return i&&(e+=`?legacyResourceID=${i}`),e}static async deleteConfigCache(e,t,i){const s=c.buildCacheID(e,i,t);this.configById.delete(s)}static buildCacheID(e,t,i){return null==e?`req_${t}_${i}`:`req_${t}_${e}_${i}`}}c.LEGACY_CONFIG_NAME_ERROR_MESSAGE="LegacyConfigName precisa ser informado.",c.filterBarLegacyConfig=new Map,c.formLegacyConfig=new Map,c.gridLegacyConfig=new Map,c.configById=new Map,c.filterBarConfigFetcher=new class extends e{normalize(e){return e.map((e=>{const{id:t,value:i,fixed:s,visible:r,type:a,groupedItems:n}=e,o={id:t};if(i&&(o.value=l(i,a)),s&&(o.fixed=s),n){if(0===n.length)return;n.filter((e=>e.visible)).length>0&&(o.visible=!0),o.groupedItems=this.normalize(n)}else r&&(o.visible=!0);return o})).filter((e=>null!=e))}saveConfig(e,t,i){return this.saveResource(this.normalize(e),this.getPath(t,i))}getConfig(e,t,i){return new Promise(((s,r)=>{this.loadResource(this.getPath(e,t,i)).then((e=>{let t;if(e){const i=JSON.parse(e);t=this.buildFieldList(i.items)}s(t||[])})).catch((e=>{r(e)}))}))}buildFieldList(e){return e.map((e=>{var t,i;if(e.type===r.MULTI_LIST){const s=e.value;let r=null!==(i=null!==(t=null==s?void 0:s.elements)&&void 0!==t?t:null==s?void 0:s.members)&&void 0!==i?i:s;r=null!=r?r:[],r=Array.isArray(r)?r:[r],r=r.map((e=>Object.assign(Object.assign({},e),{id:void 0!==e.id?e.id:e.value}))),e.value=a.copy(r)}return e}))}getPath(e,t,i){let s=`cfg://filter/FilterBarState:${e}`;return t&&(s+=`.${t}`),i&&(s+=this.buildQueryString(i)),s}buildQueryString(e){let t="?";for(let i in e)e.hasOwnProperty(i)&&(t.length>1&&(t+="&"),t+=i+"="+e[i]);return t}},c.formConfigFetcher=new t,c.gridConfigFetcher=new class extends e{constructor(){super(...arguments),this.GRID_CONFIG_VERSION="V3:"}async getDefaultConfig(e){var t,s;const r={requestBody:{gridConfig:{configName:`GrdCfgHtml5:${e}`}}},a=await i.get().callServiceBroker("GridConfig.restoreGridConfigFromJson",JSON.stringify(r));return{columns:null!==(s=null===(t=null==a?void 0:a.gridConfig)||void 0===t?void 0:t.field)&&void 0!==s?s:[]}}getConfig(e,t){const i=this.getPath(t,e);return new Promise(((e,t)=>{this.loadResource(i).then((t=>{let i;t&&(i=JSON.parse(t)),e(i)})).catch((e=>{t(e)}))}))}saveConfig(e,t,i){const s=this.getPath(i,t);return new Promise(((t,i)=>{this.saveResource(e,s).then((e=>{t(JSON.parse(e.resource))})).catch((e=>{i(e)}))}))}getPath(e,t){return t?`cfg://grid/${this.GRID_CONFIG_VERSION}${e}/${t}`:`cfg://grid/${this.GRID_CONFIG_VERSION}${e}`}},c.personalizedFilterFetcher=new o;export{c as C,o as P}