import{L as Lang}from"./lang-6ad49f2b.js";import{E as Errors}from"./errors-f0c693af.js";import{b as alertCodeByStatus}from"./helpers-82cb299a.js";var DialogService=function(){function n(){}n.prototype.onAction=function(n){if(!n){return}if(n==="reload"||n==="refresh"){location.reload()}if("".concat(n).startsWith("redirect:")){location.href="".concat(n).replace("redirect:","")}};n.prototype.createModal=function(){var n=document.createElement("x-modal");n.size="small";n.closable=false;document.body.appendChild(n);return n};n.prototype.createDialog=function(n){var e=document.createElement("x-dialog");e.message=n.message;n.type&&(e.type=n.type);n.header&&(e.header=n.header);n.buttonCloseText&&(e.buttonCloseText=n.buttonCloseText);n.buttonConfirmYesText&&(e.buttonConfirmYesText=n.buttonConfirmYesText);n.buttonConfirmNoText&&(e.buttonConfirmNoText=n.buttonConfirmNoText);n.buttonConfirmLoading&&(e.buttonConfirmLoading=n.buttonConfirmLoading);n.buttonConfirmResult&&(e.buttonConfirmResult=n.buttonConfirmResult);n.interval&&(e.interval=n.interval);return e};n.prototype.open=function(n){var e=this;return new Promise((function(t){if(typeof n==="string"){n={message:n}}if(typeof n==="object"&&n.type&&!n.header){n.header=Lang.get("alert.".concat(n.type))||null}var r=e.createModal();var o=e.createDialog(n);r.appendChild(o);r.open();o.addEventListener("close",(function(){r.remove();t("close")}));o.addEventListener("confirm",(function(){r.remove();t("confirm")}));o.addEventListener("refuse",(function(){r.remove();t("refuse")}))}))};n.prototype.fromResponse=function(n){var e=this;return new Promise((function(t,r){if(!(n===null||n===void 0?void 0:n.data)||!(n===null||n===void 0?void 0:n.status)){return}if(n.data.errors){Errors.fill(n.data.errors)}if(n.data&&(n.status>=200&&n.status<300)){e.onAction(n.data.action);t(n.data)}else{e.open({message:n.message||n.data.message,type:alertCodeByStatus(n.status)}).then((function(){if(n.data.action){e.onAction(n.data.action)}r(n.data)}))}}))};n.create=function(){var e=new n;return Object.assign((function(n){return e.open(n)}),{fromResponse:function(n){return e.fromResponse(n)},success:function(n){return e.open(Object.assign({type:"success"},n))},danger:function(n){return e.open(Object.assign({type:"danger"},n))},exception:function(n){return e.open(Object.assign({type:"exception"},n))},error:function(n){return e.open(Object.assign({type:"error"},n))},invalid:function(n){return e.open(Object.assign({type:"invalid"},n))},missing:function(n){return e.open(Object.assign({type:"missing"},n))},payment:function(n){return e.open(Object.assign({type:"payment"},n))},unknown:function(n){return e.open(Object.assign({type:"unknown"},n))},confirm:function(n){return e.open(Object.assign({type:"confirm"},n))},failure:function(n){return e.open(Object.assign({type:"failure"},n))},noResult:function(n){return e.open(Object.assign({type:"no-result"},n))},forbidden:function(n){return e.open(Object.assign({type:"forbidden"},n))}})};return n}();var Dialog=DialogService.create();export{Dialog as D};