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