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

export class ConfigManager {
    constructor(pluginOptions, table) {
        this.pluginOptions = pluginOptions, this.table = table, this.isRowExpanded = () => !1;
    }
    hasChildren(record) {
        if (record && "object" == typeof record && "children" in record) {
            const children = record.children;
            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();
    }
    getDataCount() {
        var _a, _b, _c, _d, _e;
        return this.table.pagination ? null !== (_c = null === (_b = null === (_a = this.table.dataSource) || void 0 === _a ? void 0 : _a.currentPagerIndexedData) || void 0 === _b ? void 0 : _b.length) && void 0 !== _c ? _c : 0 : null !== (_e = null === (_d = this.table.dataSource) || void 0 === _d ? void 0 : _d.sourceLength) && void 0 !== _e ? _e : 0;
    }
    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;
        (recordList => {
            recordList.forEach((record => {
                if (record && "object" == typeof record) {
                    const recordObj = record;
                    (this.hasChildren(record) || !0 === recordObj.children) && (recordObj.hierarchyState = HierarchyState.collapse);
                }
            }));
        })(records);
    }
    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