import{L as e}from"./p-a0c4df1d.js";import{E as t}from"./p-c38765d6.js";import{b as o}from"./p-9c6e275c.js";class r{onAction(e){e&&("reload"!==e&&"refresh"!==e||location.reload(),`${e}`.startsWith("redirect:")&&(location.href=`${e}`.replace("redirect:","")))}createModal(){const e=document.createElement("x-modal");return e.size="small",e.closable=!1,document.body.appendChild(e),e}createDialog(e){const t=document.createElement("x-dialog");return t.message=e.message,e.type&&(t.type=e.type),e.header&&(t.header=e.header),e.buttonCloseText&&(t.buttonCloseText=e.buttonCloseText),e.buttonConfirmYesText&&(t.buttonConfirmYesText=e.buttonConfirmYesText),e.buttonConfirmNoText&&(t.buttonConfirmNoText=e.buttonConfirmNoText),e.buttonConfirmLoading&&(t.buttonConfirmLoading=e.buttonConfirmLoading),e.buttonConfirmResult&&(t.buttonConfirmResult=e.buttonConfirmResult),e.interval&&(t.interval=e.interval),t}open(t){return new Promise((o=>{"string"==typeof t&&(t={message:t}),"object"==typeof t&&t.type&&!t.header&&(t.header=e.get(`alert.${t.type}`)||null);const r=this.createModal(),n=this.createDialog(t),s=e=>{n.removeEventListener("close",s),n.removeEventListener("confirm",s),n.removeEventListener("refuse",s),r.remove(),o(e.type)};r.appendChild(n),r.open(),n.addEventListener("close",s),n.addEventListener("confirm",s),n.addEventListener("refuse",s)}))}fromResponse(e){return new Promise(((r,n)=>{var s;(null==e?void 0:e.data)&&(null==e?void 0:e.status)&&(e.data.errors&&t.fill(e.data.errors),e.data&&e.status>=200&&e.status<300?(this.onAction(e.data.action),r(e.data)):this.open({message:null!==(s=e.message)&&void 0!==s?s:e.data.message,type:o(e.status)}).then((()=>{e.data.action&&this.onAction(e.data.action),n(e.data)})))}))}static create(){const e=new r;return Object.assign((t=>e.open(t)),{fromResponse:t=>e.fromResponse(t),success:t=>e.open(Object.assign({type:"success"},t)),danger:t=>e.open(Object.assign({type:"danger"},t)),exception:t=>e.open(Object.assign({type:"exception"},t)),error:t=>e.open(Object.assign({type:"error"},t)),invalid:t=>e.open(Object.assign({type:"invalid"},t)),missing:t=>e.open(Object.assign({type:"missing"},t)),payment:t=>e.open(Object.assign({type:"payment"},t)),unknown:t=>e.open(Object.assign({type:"unknown"},t)),confirm:t=>e.open(Object.assign({type:"confirm"},t)),failure:t=>e.open(Object.assign({type:"failure"},t)),noResult:t=>e.open(Object.assign({type:"no-result"},t)),forbidden:t=>e.open(Object.assign({type:"forbidden"},t))})}}const n=r.create();export{n as D}