var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
define(["require", "exports", "jquery", "./ProcessModal"], function (require, exports, jquery_1, ProcessModal_1) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    jquery_1 = __importDefault(jquery_1);
    ProcessModal_1 = __importDefault(ProcessModal_1);
    const TITLE = 'Перевірка документу';
    const HELP_CONTINUE = 'Продовжити з зауваженями?';
    const ACTION_CONTINUE = 'Продовжити';
    const ERROR_URL_CHECK = 'Сталась помилка при перевірці';
    const ERROR_SERVER = "Відсутній зв'язок з сервером";
    class FullnessModal extends ProcessModal_1.default {
        static create(props) {
            return new FullnessModal(props);
        }
        constructor(props) {
            super({
                title: props.modalTitle || TITLE,
                actionText: ACTION_CONTINUE,
                actionCallback: () => this._setSuccess(),
            });
            this.onProcess = false;
            this.props = props;
        }
        _setSuccess() {
            this.modal.onClose(() => {
                this.props.onSuccess(this.resp);
            });
            this.modal.close();
        }
        _setBodyText(errorList) {
            const errorText = errorList
                .map((value) => {
                return `<li class='media errorsListItem'><div class='mr-3'><a class='btn bg-transparent border-warning text-warning rounded-round border-2 btn-icon'><i class='icon-alert'></i></a></div><div class='media-body'>${value}</div></li>`;
            })
                .join('');
            this.modalBodyForm.html(`<ul class='media-list mt-2'>${errorText}</ul>`);
        }
        _fetchCheck() {
            this.showLoader(this.props.modalTitle || TITLE);
            this.modalBodyForm.empty();
            this.hideAction();
            if (this.onProcess) {
                return;
            }
            this.onProcess = true;
            const reqProps = { url: this.props.urlCheck, method: 'POST', data: this.props.bodyData };
            if (this.props.asFormData) {
                reqProps.processData = false;
                reqProps.contentType = false;
            }
            jquery_1.default.ajax(reqProps)
                .always(() => {
                this.onProcess = false;
                this.hideLoader();
            })
                .done((data) => {
                this.resp = data;
                if (data && data.ok !== null) {
                    if (data.ok) {
                        this._setSuccess();
                    }
                    else {
                        if (data.msg) {
                            this.setError(data.msg);
                        }
                        if (data.errors) {
                            this._setBodyText(data.errors);
                        }
                        if (data.canContinue === true) {
                            this.modal.setTitle(HELP_CONTINUE);
                            this.showAction();
                        }
                    }
                }
                else {
                    this.setError(ERROR_URL_CHECK);
                }
            })
                .fail(() => this.setError(ERROR_SERVER))
                .always(() => {
                this.props.onFinally && this.props.onFinally();
            });
        }
        open() {
            super.openModal();
            this._fetchCheck();
        }
    }
    exports.default = FullnessModal;
});
