import { L as Lang } from './lang-3a0141b1.js';
import { E as Errors } from './errors-2db24f13.js';
import { b as alertCodeByStatus } from './helpers-7608753a.js';

class DialogService {
    /**
     * Action handler from message response
     *
     * @param action
     * @returns
     */
    onAction(action) {
        if (!action) {
            return;
        }
        if (action === 'reload' || action === 'refresh') {
            location.reload();
        }
        if (`${action}`.startsWith('redirect:')) {
            location.href = `${action}`.replace('redirect:', '');
        }
    }
    /**
     * Create modal element
     *
     * @returns
     */
    createModal() {
        const modal = document.createElement('x-modal');
        modal.size = 'small';
        modal.closable = false;
        document.body.appendChild(modal);
        return modal;
    }
    /**
     * Create dialog element
     *
     * @param options
     * @returns
     */
    createDialog(options) {
        const dialog = document.createElement('x-dialog');
        dialog.message = options.message;
        options.type && (dialog.type = options.type);
        options.header && (dialog.header = options.header);
        options.buttonCloseText && (dialog.buttonCloseText = options.buttonCloseText);
        options.buttonConfirmYesText && (dialog.buttonConfirmYesText = options.buttonConfirmYesText);
        options.buttonConfirmNoText && (dialog.buttonConfirmNoText = options.buttonConfirmNoText);
        options.buttonConfirmLoading && (dialog.buttonConfirmLoading = options.buttonConfirmLoading);
        options.buttonConfirmResult && (dialog.buttonConfirmResult = options.buttonConfirmResult);
        options.interval && (dialog.interval = options.interval);
        return dialog;
    }
    /**
     * Open dialog and show message
     *
     * @param options
     * @returns
     */
    open(options) {
        return new Promise((resolve) => {
            if (typeof options === 'string') {
                options = {
                    message: options,
                };
            }
            if (typeof options === 'object' && options.type && !options.header) {
                options.header = Lang.get(`alert.${options.type}`) || null;
            }
            const modal = this.createModal();
            const dialog = this.createDialog(options);
            const onevent = (ev) => {
                dialog.removeEventListener('close', onevent);
                dialog.removeEventListener('confirm', onevent);
                dialog.removeEventListener('refuse', onevent);
                modal.remove();
                resolve(ev.type);
            };
            modal.appendChild(dialog);
            modal.open();
            dialog.addEventListener('close', onevent);
            dialog.addEventListener('confirm', onevent);
            dialog.addEventListener('refuse', onevent);
        });
    }
    /**
     * Create response dialog from request
     *
     * @param response
     * @returns
     */
    fromResponse(response) {
        return new Promise((resolve, reject) => {
            var _a;
            if (!(response === null || response === void 0 ? void 0 : response.data) || !(response === null || response === void 0 ? void 0 : response.status)) {
                return;
            }
            if (response.data.errors) {
                Errors.fill(response.data.errors);
            }
            if (response.data && (response.status >= 200 && response.status < 300)) {
                this.onAction(response.data.action);
                resolve(response.data);
            }
            else {
                this.open({
                    message: (_a = response.message) !== null && _a !== void 0 ? _a : response.data.message,
                    type: alertCodeByStatus(response.status),
                }).then(() => {
                    if (response.data.action) {
                        this.onAction(response.data.action);
                    }
                    reject(response.data);
                });
            }
        });
    }
    /**
     * Create service
     *
     * @returns
     */
    static create() {
        const instance = new DialogService();
        return Object.assign((options) => instance.open(options), {
            fromResponse: (response) => instance.fromResponse(response),
            success: (options) => instance.open(Object.assign({ type: 'success' }, options)),
            danger: (options) => instance.open(Object.assign({ type: 'danger' }, options)),
            exception: (options) => instance.open(Object.assign({ type: 'exception' }, options)),
            error: (options) => instance.open(Object.assign({ type: 'error' }, options)),
            invalid: (options) => instance.open(Object.assign({ type: 'invalid' }, options)),
            missing: (options) => instance.open(Object.assign({ type: 'missing' }, options)),
            payment: (options) => instance.open(Object.assign({ type: 'payment' }, options)),
            unknown: (options) => instance.open(Object.assign({ type: 'unknown' }, options)),
            confirm: (options) => instance.open(Object.assign({ type: 'confirm' }, options)),
            failure: (options) => instance.open(Object.assign({ type: 'failure' }, options)),
            noResult: (options) => instance.open(Object.assign({ type: 'no-result' }, options)),
            forbidden: (options) => instance.open(Object.assign({ type: 'forbidden' }, options)),
        });
    }
}
const Dialog = DialogService.create();

export { Dialog as D };
