import{r as t,h as e}from"./p-15c07fa9.js";import{DataUnit as s,UserInterface as i,DataType as r,ApplicationContext as a}from"@sankhyalabs/core";import{P as n}from"./p-eaad0aa8.js";const o=class{constructor(e){t(this,e),this.APPLY="OK",this.CANCEL="CANCEL",this.CLOSE="CLOSE",this.action=void 0,this.applyParameters=void 0,this.dataUnit=new s}async openPopup(){this._ezPopup.opened=!0}exitPopup(){this._ezPopup.opened=!1}async handleApply(){try{await this._ezForm.validate();let t=this.dataUnit.getSelectedRecord();t&&(this._params.forEach(((e,s)=>{if(t.hasOwnProperty(e.name)){if(e.paramType==n.ENTITY){let s=t[e.name];return s?void(e.value="object"==typeof s?s.value:s):void(e.value=null)}this._params[s].value=t[e.name]}else this._params[s].value=void 0})),this.action.actionConfig.params.promptParam=this._params),this.exitPopup(),this.applyParameters(this.action)}catch(t){}}searchLoader(t,e){return this._application.executePreparedSearch(t.mode,t.argument,{entity:e.entityName,entityDescription:e.label})}getUserInterface(t){switch(t){case n.TEXT:return i.SHORTTEXT;case n.INTEGER:return i.INTEGERNUMBER;case n.DECIMAL:return i.DECIMALNUMBER;case n.DATE:return i.DATE;case n.DATETIME:return i.DATETIME;case n.BOOLEAN:return i.CHECKBOX;case n.ENTITY:return i.SEARCH;case n.OPTIONS:return i.OPTIONSELECTOR}}getDataType(t){switch(t){case n.TEXT:case n.OPTIONS:return r.TEXT;case n.INTEGER:case n.DECIMAL:return r.NUMBER;case n.DATE:case n.DATETIME:return r.DATE;case n.BOOLEAN:return r.BOOLEAN;case n.ENTITY:return r.OBJECT}}getProps(t){switch(t.paramType){case n.DECIMAL:return t.precision?{precision:Number(t.precision)}:void 0;case n.ENTITY:return{ENTITYNAME:t.entityName};case n.OPTIONS:return{options:this.prepareOptions(t.options)};default:return}}buildFields(){let t=[];this._params.forEach(((e,s)=>{const i=!!e.name;let r=i?e.name:`param_${s}`;t.push({name:r,label:e.label,dataType:this.getDataType(e.paramType),userInterface:this.getUserInterface(e.paramType),readOnly:!1,required:"B"!=e.paramType&&"true"===e.required,properties:this.getProps(e)}),this._params[s].name=r,this._params[s].isGeneratedName=!i})),this._metadata={name:"params",label:"params",fields:t}}fillFields(){this._params.forEach((t=>{"true"==t.saveLast&&this.dataUnit.setFieldValue(t.name,t.value)}))}prepareOptions(t){return t.split(";").map((function(t,e){let s,i;if(t.indexOf("=")>-1){let e=t.split("=");s=e[0],i=e[1]}else s=(e+1).toString(),i=t;return{value:s,label:i}}))}componentWillLoad(){this._application=a.getContextValue("__SNK__APPLICATION__"),this._params=[...this.action.actionConfig.params.promptParam],this.dataUnit.metadataLoader=this.metadataLoader.bind(this),this.dataUnit.loadMetadata().then((()=>{this.fillFields()})),this.dataUnit.addRecord()}async metadataLoader(){return new Promise((t=>{this.buildFields(),t(this._metadata)}))}handleModalAction(t){switch(t){case this.APPLY:this.handleApply();break;case this.CANCEL:case this.CLOSE:this.exitPopup()}}render(){return e("ez-popup",{size:"medium",heightMode:"auto",useHeader:!1,ref:t=>this._ezPopup=t},e("ez-modal-container",{modalTitle:this._application.messagesBuilder.getMessage("snkActionsButton.form.fillParams",void 0),okButtonLabel:this._application.messagesBuilder.getMessage("snkActionsButton.buttons.apply",void 0),cancelButtonLabel:this._application.messagesBuilder.getMessage("snkActionsButton.buttons.cancel",void 0),onEzModalAction:t=>this.handleModalAction(t.detail)},e("div",{class:"ez-margin-vertical--medium"},e("ez-form",{ref:t=>this._ezForm=t,dataUnit:this.dataUnit}))))}};export{o as snk_actions_form}