var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
define(["require", "exports", "jquery", "bootstrap/js/dist/modal", "./ProcessModal", "./FullnessModal"], function (require, exports, jquery_1, modal_1, ProcessModal_1, FullnessModal_1) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    exports.ModalInstance = exports.FullnessModal = exports.ProcessModal = exports.ModalStyle = exports.ModalSize = void 0;
    jquery_1 = __importDefault(jquery_1);
    modal_1 = __importDefault(modal_1);
    ProcessModal_1 = __importDefault(ProcessModal_1);
    FullnessModal_1 = __importDefault(FullnessModal_1);
    exports.ProcessModal = ProcessModal_1.default;
    exports.FullnessModal = FullnessModal_1.default;
    const loadingModal = '<div class="progress progress-striped active"><div class="progress-bar progress-bar-info" style="width: 100%;"></div></div>';
    const template = '<div class="modal fade" aria-modal="true"><div class="modal-dialog modal-dialog-centered modal-dialog-scrollable"><div class="modal-content"><div class="modal-header"><h5 class="modal-title">Basic modal</h5><button type="button" class="close" data-bs-dismiss="modal">&times;</button></div><div class="modal-body"></div></div></div></div>';
    var ModalSize;
    (function (ModalSize) {
        ModalSize["xs"] = "modal-xs";
        ModalSize["sm"] = "modal-sm";
        ModalSize["md"] = "";
        ModalSize["lg"] = "modal-lg";
        ModalSize["full"] = "modal-full";
    })(ModalSize = exports.ModalSize || (exports.ModalSize = {}));
    var ModalStyle;
    (function (ModalStyle) {
        ModalStyle["primary"] = "bg-primary";
        ModalStyle["danger"] = "bg-danger";
        ModalStyle["success"] = "bg-success";
        ModalStyle["warning"] = "bg-warning";
        ModalStyle["info"] = "bg-info";
    })(ModalStyle = exports.ModalStyle || (exports.ModalStyle = {}));
    function htmlToElement(html) {
        const template = document.createElement('template');
        html = html.trim(); // Never return a text node of whitespace as the result
        template.innerHTML = html;
        return template.content.firstChild;
    }
    class ModalInstance {
        static create(props) {
            return new ModalInstance(props);
        }
        constructor(props) {
            this.asBackdrop = false;
            this.dialog = (0, jquery_1.default)(template);
            if (props && props.withoutAnimation) {
                this.dialog.removeClass('fade');
            }
            this.modal = new modal_1.default(this.dialog[0]);
            (0, jquery_1.default)('body').append(this.dialog);
        }
        destroy() {
            this.modal.dispose();
            this.dialog.remove();
        }
        open(title, size, style, asStatic) {
            this.setSize(size);
            this.setStyle(style);
            this.setTitle(title);
            this.setBody(loadingModal);
            this.removeFooter();
            if (asStatic) {
                this.modal.dispose();
                this.modal = new modal_1.default(this.dialog[0], { backdrop: 'static' });
                this.asBackdrop = true;
            }
            else if (this.asBackdrop) {
                this.modal.dispose();
                this.modal = new modal_1.default(this.dialog[0]);
                this.asBackdrop = false;
            }
            this.modal.show();
            return this;
        }
        close() {
            // @ts-ignore
            if (this.modal._isTransitioning) {
                this.onOpen(() => this.modal.hide());
            }
            else {
                this.modal.hide();
            }
        }
        onOpen(action) {
            this.dialog[0].addEventListener('shown.bs.modal', () => action(), { once: true });
            return this;
        }
        onClose(action) {
            this.dialog[0].addEventListener('hidden.bs.modal', () => action(), { once: true });
            return this;
        }
        setStyle(style) {
            if (style !== this.lastStyle) {
                const header = this.dialog[0].querySelector('.modal-header');
                if (header) {
                    header.setAttribute('class', 'modal-header'); //reset classList
                    this.lastStyle = style;
                    if (style) {
                        if (header.classList) {
                            header.classList.add(style);
                        }
                        else {
                            header.className += ' ' + style;
                        }
                    }
                }
            }
            return this;
        }
        setSize(modalSize) {
            if (modalSize !== this.lastSize) {
                const dialog = this.dialog[0].querySelector('.modal-dialog');
                if (dialog) {
                    this.lastSize = modalSize || ModalSize.md;
                    dialog.className = ('modal-dialog ' + this.lastSize).trim();
                }
            }
            return this;
        }
        setTitle(content) {
            if (content !== this.lastTitle) {
                const header = this.dialog.find('.modal-header');
                if (content) {
                    header.show();
                    const title = header.find('.modal-title');
                    title.html(content);
                    this.lastTitle = content;
                }
                else {
                    header.hide();
                }
            }
            return this;
        }
        setBody(content, withNoPadding = false) {
            const body = this.dialog.find('.modal-body');
            body.empty().append(content);
            body.toggleClass('no-padding', withNoPadding);
            return this;
        }
        getBody() {
            return this.dialog.find('.modal-body');
        }
        setFooter(newContent) {
            const modalContent = this.dialog.find('.modal-content');
            if (modalContent.find('.modal-footer').length > 0) {
                modalContent.find('.modal-footer')
                    .empty();
            }
            else {
                modalContent.append('<div class="modal-footer"/>');
            }
            if (newContent) {
                modalContent.find('.modal-footer')
                    .append(newContent);
            }
            else {
                modalContent.find('.modal-footer')
                    .html('<button type="button" class="btn btn-light" data-bs-dismiss="modal">Закрити</button>');
            }
            return this;
        }
        getFooter() {
            return this.dialog.find('.modal-footer');
        }
        removeFooter() {
            const footer = this.getFooter();
            if (footer) {
                footer.remove();
            }
        }
        setWithoutAnimation(set) {
            this.dialog.toggleClass('fade', !set);
        }
    }
    exports.ModalInstance = ModalInstance;
    const modal = ModalInstance.create();
    exports.default = modal;
});
