import{d as e,D as t}from"./p-b2212d20.js";import{ObjectUtils as n}from"@sankhyalabs/core";class o{constructor(){this.templateByQuery=new Map,this.buldTemplates()}buldTemplates(){this.templateByQuery.set("fetchResource",e.gql`query($name: String!) {
                    $queryAlias$: fetchResource(name: $name){
                        resource
                    }
                  }`),this.templateByQuery.set("saveResource",e.gql`mutation($resource: InputResource!) {
                $queryAlias$: saveResource(resource: $resource){
                    name
                    resource                    
                }
              }`)}loadResource(e){if(o._loadingResource.has(e))return o._loadingResource.get(e);const n=new Promise(((n,r)=>{t.get().callGraphQL({values:{name:e},query:this.templateByQuery.get("fetchResource")}).then((t=>{n(null==t?void 0:t.resource),o._loadingResource.delete(e)})).catch((t=>{r(t),o._loadingResource.delete(e)}))}));return o._loadingResource.set(e,n),n}saveResource(e,n){return new Promise(((o,r)=>{t.get().callGraphQL({values:{resource:{name:n,resource:JSON.stringify(e)}},query:this.templateByQuery.get("saveResource")}).then((e=>{o(e)})).catch((e=>{r(e)}))}))}}o._loadingResource=new Map;class r extends o{constructor(){super(),this.queryConfig=new Map,this.buildTemplatesConfig()}buildTemplatesConfig(){this.queryConfig.set("fetchUserAvailableConfigs",e.gql`query($name: String!) {
                    $queryAlias$: fetchUserAvailableConfigs(name: $name){
                        name
                        origin
                        key
                  }
                }`),this.queryConfig.set("fetchLegacyConfig",e.gql`query($name: String!) {
                    $queryAlias$: fetchLegacyConfig(name: $name){
                        name
                        resource
                  }
                }`),this.queryConfig.set("fetchDefaultConfig",e.gql`query($name: String!) {
                    $queryAlias$: fetchDefaultConfig(name: $name){
                        name
                        resource
                  }
                }`)}loadFormConfig(e,t){return new Promise(((n,o)=>{Promise.all([this.loadResource(this.getPath(t,e)),this.loadResource(this.getPath(`FormCardState:${t}`,e,"form-card")),this.loadResource(this.getPath(t,e,"summary")),this.loadResource(this.getPath(t,e,"defaultValues"))]).then((([e,t,o,r])=>{let i;if(null!=e){i=Object.assign(Object.assign({},JSON.parse(e)),{emptyConfig:!1});const{tabs:t,fields:n}=i;if(t){const e=new Map(t.map((e=>[e.label,e])));null==n||n.forEach((t=>{var n;return t.tab=(null===(n=e.get(this.getTabName(t.tab)))||void 0===n?void 0:n.label)||e.get(this.getTabName(t.tab))}))}i.fields=null==n?void 0:n.map((e=>{if("readonly"in e){const t=!0===e.readonly;delete e.readonly,e.readOnly=t}return e}))}null!=t&&(null==i&&(i={emptyConfig:!0,fields:[]}),i.cardsState=new Map(JSON.parse(t))),null!=o&&(null==i&&(i={emptyConfig:!0,fields:[]}),i.summary=new Map(Object.entries(JSON.parse(o)))),null!=r&&(null==i&&(i={emptyConfig:!0,fields:[]}),i.defaultVars=new Map(Object.entries(JSON.parse(r)))),n(i)})).catch((e=>{o(e)}))}))}getTabName(e){return"object"==typeof e?e.label:e}saveCardState(e,t,n){const o=this.getPath(`FormCardState:${n}`,t,"form-card");return new Promise(((t,n)=>{this.saveResource(Array.from(e.entries()),o).then((e=>{t(new Map(JSON.parse(e.resource)))})).catch((e=>{n(e)}))}))}saveConfig(e,t,n){delete e.cardsState,delete e.defaultVars,delete e.summary;const o=this.getPath(n,t);return new Promise(((t,n)=>{this.saveResource(e,o).then((e=>{t(JSON.parse(e.resource))})).catch((e=>{n(e)}))}))}fetchUserAvailableConfigs(e,n){const o=this.getPath(n,e);return new Promise(((e,n)=>{t.get().callGraphQL({values:{name:o},query:this.queryConfig.get("fetchUserAvailableConfigs")}).then((t=>{e(t)})).catch((e=>{n(e)}))}))}fetchLegacyConfig(e,o){const r=this.getPath(o,e);return new Promise(((e,o)=>{t.get().callGraphQL({values:{name:r},query:this.queryConfig.get("fetchLegacyConfig")}).then((t=>{e(n.stringToObject((null==t?void 0:t.resource)||""))})).catch((e=>{o(e)}))}))}fetchDefaultConfig(e,o){const r=this.getPath(o,e);return new Promise(((e,o)=>{t.get().callGraphQL({values:{name:r},query:this.queryConfig.get("fetchDefaultConfig")}).then((t=>{(null==t?void 0:t.resource)||e(null),e(n.stringToObject(null==t?void 0:t.resource))})).catch((e=>{o(e)}))}))}getPath(e,t,n="form"){return t?`cfg://${n}/${e}/${t}`:`cfg://${n}/${e}`}async fetchLegacyConfigurableForm(e){const o={requestBody:{config:{chave:e,tipo:"T"}}},r=await t.get().callServiceBroker("SystemUtilsSP.getConf",JSON.stringify(o));if(!n.isEmpty(r))return this.buildLegacyConfigurableForm(r)}async fetchSimpleFormConfig(e){const n={requestBody:{formConfig:{configName:e}}},o=await t.get().callServiceBroker("FormConfig.getFormConfig",JSON.stringify(n));return this.processConfig(o)}async saveSimpleFormConfig(e,n){const o=this.buildSaveSimpleFormConfigPayload(e,n),r=await t.get().callServiceBroker("FormConfig.saveFormConfig",JSON.stringify(o));this.processConfig(r)}processConfig(e){return n.isEmpty(e)?[]:this.getResponseAsArray(e).map((e=>e.name))}getResponseAsArray(e){var t,n,o;const r=null!==(o=null===(n=null===(t=null==e?void 0:e.formConfig)||void 0===t?void 0:t.layout)||void 0===n?void 0:n.field)&&void 0!==o?o:[];return Array.isArray(r)?r:[r]}buildSaveSimpleFormConfigPayload(e,t){return{formConfig:{configName:e,layout:{field:t.map((e=>({name:e})))},metadata:{field:[]}}}}async restoreFormConfig(e){const n={requestBody:{formConfig:{configName:e}}};await t.get().callServiceBroker("FormConfig.restoreFormConfig",JSON.stringify(n))}buildLegacyConfigurableForm(e){var t,n,o;const r=null!==(o=null===(n=null===(t=null==e?void 0:e.formConfig)||void 0===t?void 0:t.layout)||void 0===n?void 0:n.field)&&void 0!==o?o:[];return{emptyConfig:!1,fields:Array.isArray(r)?r:[r]}}async fetchLayoutFormConfig(e){const o={requestBody:{config:{chave:e,tipo:"T"}}},r=await t.get().callServiceBroker("SystemUtilsSP.getConf",JSON.stringify(o));if(!n.isEmpty(r))return r.config}async saveLayoutFormConfig(e,o){const r={requestBody:{config:Object.assign(Object.assign({},o),{chave:e,tipo:"T"})}},i=await t.get().callServiceBroker("SystemUtilsSP.saveConf",JSON.stringify(r));if(!n.isEmpty(i))return i}}var i;!function(e){e.USER="USER",e.DEFAULT="DEFAULT",e.SHARED="SHARED"}(i||(i={}));export{r as F,o as R,i as U}