import * as VTable from "@visactor/vtable";

export class ConfigManager {
    constructor(pluginOptions, table) {
        this.pluginOptions = pluginOptions, this.table = table, this.isRowExpanded = () => !1, 
        this.childrenKey = pluginOptions.childrenKey || "children";
    }
    getChildrenKey() {
        return this.childrenKey;
    }
    setExpandRowCallback(callback) {
        this.expandRowCallback = callback;
    }
    ensureChildrenAlias(record) {
        if ("children" !== this.childrenKey && record && "object" == typeof record) {
            const recordObj = record, key = this.childrenKey, descriptor = Object.getOwnPropertyDescriptor(recordObj, "children");
            if (descriptor && descriptor.get) return;
            Object.defineProperty(recordObj, "children", {
                get() {
                    return this[key];
                },
                set(value) {
                    this[key] = value;
                },
                enumerable: !1,
                configurable: !0
            });
        }
    }
    hasChildren(record) {
        const key = this.childrenKey;
        if (record && "object" == typeof record && key in record) {
            const children = record[key];
            return Array.isArray(children) && children.length > 0;
        }
        return !1;
    }
    injectMasterDetailOptions(options) {
        options.masterDetail = !0, options.customConfig || (options.customConfig = {}), 
        options.customConfig.scrollEventAlwaysTrigger = !0;
        const originalCustomComputeRowHeight = options.customComputeRowHeight;
        if (options.customComputeRowHeight = params => {
            var _a;
            const {row: row, table: table} = params;
            if (this.isRowExpanded(row)) {
                const expandedHeight = table.getRowHeight(row);
                return Array.isArray(expandedHeight) ? null !== (_a = expandedHeight[0]) && void 0 !== _a ? _a : "auto" : expandedHeight;
            }
            if (originalCustomComputeRowHeight) {
                const userResult = originalCustomComputeRowHeight(params);
                if (null != userResult) return userResult;
            }
        }, options.columns && options.columns.length > 0) {
            options.columns[0].tree = !0;
        }
        if (this.setupInitializedListener(), this.pluginOptions.detailTableOptions) {
            const detailOptions = this.pluginOptions.detailTableOptions;
            "function" == typeof detailOptions ? options.getDetailGridOptions = detailOptions : options.detailTableOptions = detailOptions;
        }
        this.disableRefreshHierarchyState();
    }
    disableRefreshHierarchyState() {
        setTimeout((() => {
            const tableWithPrivateMethod = this.table;
            tableWithPrivateMethod && "function" == typeof tableWithPrivateMethod._refreshHierarchyState && (tableWithPrivateMethod._originalRefreshHierarchyState = tableWithPrivateMethod._refreshHierarchyState, 
            tableWithPrivateMethod._refreshHierarchyState = () => {});
        }), 0);
    }
    setupInitializedListener() {
        this.table.on("initialized", (() => {
            const records = this.table.dataSource.records;
            this.processRecordsHierarchyStates(records), this.table.renderWithRecreateCells();
        }));
    }
    processRecordsHierarchyStates(records) {
        const HierarchyState = VTable.TYPES.HierarchyState, hierarchyExpandLevel = this.table.options.hierarchyExpandLevel || this.table.options.headerExpandLevel;
        (recordList => {
            recordList.forEach((record => {
                if (record && "object" == typeof record) {
                    const recordObj = record;
                    this.ensureChildrenAlias(record), (this.hasChildren(record) || !0 === recordObj[this.childrenKey]) && ("expand" === recordObj.hierarchyState ? recordObj.hierarchyState = HierarchyState.expand : "collapse" === recordObj.hierarchyState ? recordObj.hierarchyState = HierarchyState.collapse : recordObj.hierarchyState || (recordObj.hierarchyState = hierarchyExpandLevel && hierarchyExpandLevel > 1 ? HierarchyState.expand : HierarchyState.collapse));
                }
            }));
        })(records), this.performInitialExpansion();
    }
    performInitialExpansion() {
        const expandableRecords = this.getExpandableRecords();
        0 !== expandableRecords.length && this.startAsyncExpansion(expandableRecords);
    }
    getExpandableRecords() {
        const dataSource = this.table.dataSource, allRecords = dataSource.source || dataSource._source || this.table.dataSource.records, HierarchyState = VTable.TYPES.HierarchyState, expandableRecords = [], tableWithInternalProps = this.table;
        tableWithInternalProps.internalProps.expandedRecordIndices || (tableWithInternalProps.internalProps.expandedRecordIndices = []);
        const expandedRecordIndices = tableWithInternalProps.internalProps.expandedRecordIndices;
        for (let recordIndex = 0; recordIndex < allRecords.length; recordIndex++) {
            const record = allRecords[recordIndex];
            if (record && "object" == typeof record) {
                const recordObj = record;
                if ((this.hasChildren(record) || !0 === recordObj[this.childrenKey]) && recordObj.hierarchyState === HierarchyState.expand) {
                    expandedRecordIndices.includes(recordIndex) || expandedRecordIndices.push(recordIndex);
                    try {
                        const bodyRowIndex = this.table.getBodyRowIndexByRecordIndex(recordIndex);
                        if (bodyRowIndex >= 0) {
                            const actualRowIndex = bodyRowIndex + this.table.columnHeaderLevelCount;
                            expandableRecords.push({
                                recordIndex: recordIndex,
                                actualRowIndex: actualRowIndex,
                                record: record
                            });
                        }
                    } catch (error) {}
                }
            }
        }
        return expandableRecords;
    }
    startAsyncExpansion(expandableRecords) {
        let currentIndex = 0;
        const processNextExpansion = () => {
            if (currentIndex >= expandableRecords.length) return;
            const {actualRowIndex: actualRowIndex} = expandableRecords[currentIndex];
            this.isCellGroupCreated(actualRowIndex) ? (this.expandRowCallback && this.expandRowCallback(actualRowIndex), 
            currentIndex++, setTimeout(processNextExpansion, 0)) : setTimeout(processNextExpansion, 16);
        };
        setTimeout(processNextExpansion, 0);
    }
    isCellGroupCreated(rowIndex) {
        try {
            const cellGroup = this.table.scenegraph.getCell(0, rowIndex);
            return cellGroup && "cell" === cellGroup.role;
        } catch (error) {
            return !1;
        }
    }
    getDetailConfigForRecord(record, bodyRowIndex) {
        const detailOptions = this.pluginOptions.detailTableOptions;
        return detailOptions ? "function" == typeof detailOptions ? detailOptions({
            data: record,
            bodyRowIndex: bodyRowIndex
        }) : detailOptions : null;
    }
    setRowExpandedChecker(checker) {
        this.isRowExpanded = checker;
    }
    release() {
        this.isRowExpanded = () => !1, this.table = null, this.pluginOptions = null;
    }
}
//# sourceMappingURL=config.js.map
