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