var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
define(["require", "exports", "jquery"], function (require, exports, jquery_1) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    jquery_1 = __importDefault(jquery_1);
    const skipSize = 20000;
    const partSize = 2000;
    class SABigText {
        static create(props) {
            return new SABigText(props);
        }
        constructor(props) {
            var _a;
            this.props = props;
            (_a = props.form) === null || _a === void 0 ? void 0 : _a.one('submit', (ev) => {
                if (this.isNeedProcess()) {
                    ev.preventDefault();
                    this.handleFormSubmit().then(() => {
                        var _a;
                        (_a = props.form) === null || _a === void 0 ? void 0 : _a.trigger('submit');
                    });
                }
            });
        }
        handleFormSubmit() {
            return Promise.all(this.props.elementText.map((index, element) => __awaiter(this, void 0, void 0, function* () {
                if (!this.isNodeBig(element)) {
                    return Promise.resolve();
                }
                const nodeContent = this.getNodeContent(element);
                this.setNodeContent(element, 'Пожалуйста подождите, идет загрузка ...');
                const { partsCount, bigId } = yield this.registerBigId(nodeContent);
                this.setNodeDisabled(element, true);
                this.setBigId(index, bigId);
                yield this.sendText(nodeContent, partsCount, bigId);
                return Promise.resolve();
            })));
        }
        isNeedProcess() {
            return (this.props.elementText.filter((_index, element) => this.isNodeBig(element)).length > 0);
        }
        isNodeBig(element) {
            return this.getNodeContent(element).length >= skipSize;
        }
        getNodeContent(element) {
            if (this.props.onGetContent) {
                return this.props.onGetContent(element.id);
            }
            return element.value;
        }
        setNodeContent(element, text) {
            element.value = text;
        }
        setNodeDisabled(element, disabled) {
            element.disabled = disabled;
        }
        setBigId(indexTextElement, bigId) {
            this.props.elementBigId.eq(indexTextElement).val(bigId);
        }
        registerBigId(text) {
            return new Promise((resolve, reject) => {
                const partsCount = Math.ceil(text.length / partSize);
                const jqXHR = jquery_1.default.ajax({
                    type: 'POST',
                    url: this.props.loadUrl,
                    data: {
                        p_pac_num: partsCount
                    }
                });
                jqXHR.done((data) => {
                    if (data.ok) {
                        resolve({
                            partsCount,
                            bigId: data.id
                        });
                    }
                    else {
                        reject(new Error('not ok'));
                    }
                });
                jqXHR.fail((e) => reject(e));
            });
        }
        sendText(text, partsCount, bigId) {
            const items = Array.from({ length: partsCount }, (_, i) => i + 1);
            return Promise.all(items.map((i) => new Promise((resolve, reject) => {
                let findText;
                const indexStart = (i - 1) * partSize;
                const indexEnd = i * partSize;
                if (indexEnd > text.length) {
                    findText = text.substring(indexStart);
                }
                else {
                    findText = text.substring(indexStart, indexEnd);
                }
                const jqXHR = jquery_1.default.ajax({
                    url: this.props.loadUrl,
                    type: 'POST',
                    data: {
                        p_id: bigId,
                        p_text: findText,
                        p_pac_id: i
                    }
                });
                jqXHR.done((data) => {
                    if (data.ok) {
                        resolve();
                    }
                    else {
                        reject(new Error('not ok'));
                    }
                });
                jqXHR.fail((e) => reject(e));
            })));
        }
    }
    exports.default = SABigText;
});
