var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
define(["require", "exports", "jquery", "./index", "../../Loader"], function (require, exports, jquery_1, index_1, Loader_1) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    jquery_1 = __importDefault(jquery_1);
    Loader_1 = __importDefault(Loader_1);
    const TEXT_ERROR_DESCRIPTION = 'Опис помилки';
    const ACTION_CANCEL = 'Закрити';
    class ProcessModal {
        constructor(props1) {
            this.props1 = props1;
            this.makeModal();
            this.makeHandle();
            this.modal = index_1.ModalInstance.create({
                withoutAnimation: true
            });
        }
        destroy() {
            this.modal.destroy();
        }
        makeModal() {
            const body = (0, jquery_1.default)('<div/>');
            body.append('<div id="processLoaderBlock" style="display:none;"></div>');
            body.append('<div id="processStatusBlock" class="alert border-0 font-weight-bold font-size-lg" style="display:none;"></div>');
            body.append('<form onsubmit="return false"></form>');
            this.modalBody = body;
            this.modalBodyForm = this.modalBody.find('form');
            this.modalFooter = document.createElement('div');
            this.modalFooter.innerHTML += (`<button type='button' class='btn btn-primary'>${this.props1.actionText}</button>`);
            this.modalFooter.innerHTML += (`<button type='button' class='btn btn-light ml-2' data-bs-dismiss='modal'>${ACTION_CANCEL}</button>`);
            this.modalFooterAction = this.modalFooter.querySelector('.btn-primary');
        }
        makeHandle() {
            this.modalFooterAction.addEventListener('click', (ev) => {
                const btn = ev.target;
                if (!btn.disabled) {
                    this.setStatus();
                    this.props1.actionCallback();
                }
            });
        }
        showLoader(text) {
            const loader = this.modalBody.find('#processLoaderBlock');
            loader.show().html((0, Loader_1.default)(text));
            this.modalBodyForm.hide();
            this.modalFooterAction.disabled = true;
            this.setStatus();
        }
        hideLoader() {
            const loader = this.modalBody.find('#processLoaderBlock');
            loader.hide().empty();
            this.modalBodyForm.show();
            this.modalFooterAction.disabled = false;
        }
        setStatus(text = '', asError = false) {
            const block = this.modalBody.find('#processStatusBlock');
            block.html(text);
            block.toggle('' !== text);
            block.toggleClass('alert-success', !asError);
            block.toggleClass('alert-danger', asError);
        }
        setError(text, description = null) {
            let fullText = '';
            if (text) {
                fullText += text;
            }
            if (description) {
                fullText += `. ${TEXT_ERROR_DESCRIPTION}: `;
                fullText += typeof description === 'string' ? description : `${description.message} (${description.code})`;
            }
            this.setStatus(fullText, true);
        }
        showAction() {
            this.modalFooterAction.classList.remove('d-none');
        }
        hideAction() {
            this.modalFooterAction.classList.add('d-none');
        }
        setActionText(text) {
            this.modalFooterAction.innerHTML = text;
        }
        openModal() {
            this.modal.open(this.props1.title, index_1.ModalSize.md);
            this.modal.setBody(this.modalBody);
            this.modal.setFooter(this.modalFooter);
            this.setStatus();
        }
    }
    exports.default = ProcessModal;
});
