var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
define(["require", "exports", "jquery", "./index"], function (require, exports, jquery_1, index_1) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    jquery_1 = __importDefault(jquery_1);
    function filterByElement(row, element, rowNum) {
        const indexField = row.querySelector(element);
        return indexField && Number(indexField.getAttribute('value')) == rowNum || false;
    }
    class TableDependOne {
        static create(props) {
            return new TableDependOne(props);
        }
        constructor(props) {
            this.props = props;
            this.props.to.container.addEventListener('beforeAdd.md.table', this.beforeAppendTo.bind(this));
            this.props.to.container.addEventListener('add.md.table', this.afterAddTo.bind(this));
            this.props.from.container.addEventListener('add.md.table', this.afterAppendFrom.bind(this));
            this.props.from.container.addEventListener('delete.md.table', this.afterDeleteFrom.bind(this));
            this.props.from.container.addEventListener('oneVisible.md.table', this.afterOneVisible.bind(this));
        }
        beforeAppendTo(ev) {
            if (this.props.from.container.querySelectorAll('[data-md-table-row]').length == 0) {
                alert(this.props.emptyAlert);
                ev.stopPropagation();
            }
        }
        afterAddTo(ev) {
            var _a;
            const detail = ev.detail;
            const indexFrom = (0, index_1.getRowNum)((0, jquery_1.default)(this.props.from.container).find('[data-md-table-row]:visible')[0]);
            const newRow = Array.from(this.props.to.container.querySelectorAll('[data-md-table-row]'))
                .filter(item => (0, index_1.getRowNum)(item) == detail.newNum)
                .at(0);
            (_a = newRow === null || newRow === void 0 ? void 0 : newRow.querySelector(this.props.to.elementSelector)) === null || _a === void 0 ? void 0 : _a.setAttribute('value', indexFrom + '');
        }
        afterAppendFrom(ev) {
            const detail = ev.detail;
            if (detail.asCopy != null) {
                this.props.to.container.querySelectorAll(this.props.to.elementSelector + '[value="' + detail.asCopy + '"]')
                    .forEach((el) => {
                    const row = el.closest('[data-md-table-row]');
                    this.props.to.container.dispatchEvent(new CustomEvent('beforeCopy.md.table', {
                        bubbles: true,
                        detail: {
                            row: row,
                            toEnd: true
                        }
                    }));
                });
            }
        }
        afterDeleteFrom(ev) {
            const numToRemove = ev.detail.oldNum;
            console.log('afterDeleteFrom1', numToRemove, this.props.from.container);
            const rowNumList = [];
            this.props.to.container.querySelectorAll(this.props.to.elementSelector + '[value="' + numToRemove + '"]')
                .forEach((el) => {
                const row = el.closest('[data-md-table-row]');
                const rowNum = (0, index_1.getRowNum)(row);
                row.remove();
                rowNumList.push(rowNum);
            });
            rowNumList.reverse().forEach(num => {
                this.props.to.container.dispatchEvent(new CustomEvent('delete.md.table', {
                    detail: {
                        oldNum: num
                    }
                }));
            });
            Array.from(this.props.to.container.querySelectorAll('[data-md-table-row]'))
                .filter(row => (0, index_1.getRowNum)(row) > numToRemove)
                .forEach(row => {
                console.log('afterDeleteFrom2', row, row.cloneNode(true), this.props.from.container);
                const inputDepend = row.querySelector(this.props.to.elementSelector);
                inputDepend.setAttribute('value', String(Number(inputDepend.getAttribute('value')) - 1));
                if (rowNumList.length > 0) {
                    const inputNum = row.querySelector('[data-md-table-row-num]');
                    inputNum.innerHTML = String(Number(inputNum.innerHTML) - rowNumList.length);
                }
            });
        }
        afterOneVisible(ev) {
            const rowNum = ev.detail.rowNum;
            const toItemsAll = Array.from(this.props.to.container.querySelectorAll('[data-md-table-row]'));
            //const toIsOneVisible = TableOneVisible.isInit(this.props.to.container);
            const toIsOneVisible = this.props.to.container.getAttribute('data-one-visible') == '1';
            if (toIsOneVisible) {
                this.props.to.container.dispatchEvent(new CustomEvent('filterOneVisible.md.table', {
                    detail: {
                        filter: (row) => filterByElement(row, this.props.to.elementSelector, rowNum)
                    }
                }));
            }
            else {
                let exist = false;
                toItemsAll.forEach((row) => {
                    const found = filterByElement(row, this.props.to.elementSelector, rowNum);
                    row.style.display = found ? '' : 'none';
                    if (found) {
                        exist = true;
                    }
                });
                (0, index_1.toggleEmptyState)(this.props.to.container, !exist);
            }
        }
    }
    exports.default = TableDependOne;
});
