import { FilterPlugin, AddRowColumnPlugin, TableSeriesNumber, ContextMenuPlugin, ExcelEditCellKeyboardPlugin, AutoFillPlugin, DEFAULT_HEADER_MENU_ITEMS, DEFAULT_BODY_MENU_ITEMS, DEFAULT_COLUMN_SERIES_MENU_ITEMS } from "@visactor/vtable-plugins";

import { isValid } from "@visactor/vutils";

export function getTablePlugins(sheetDefine, options, vtableSheet) {
    var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o;
    const plugins = [], disabledPluginsUserSetted = null === (_a = null == options ? void 0 : options.VTablePluginModules) || void 0 === _a ? void 0 : _a.filter((module => module.disabled));
    let enabledPluginsUserSetted = null === (_b = null == options ? void 0 : options.VTablePluginModules) || void 0 === _b ? void 0 : _b.filter((module => !module.disabled));
    if (!(null == disabledPluginsUserSetted ? void 0 : disabledPluginsUserSetted.some((module => module.module === FilterPlugin)))) {
        const filterPlugin = createFilterPlugin(sheetDefine, null === (_c = null == enabledPluginsUserSetted ? void 0 : enabledPluginsUserSetted.find((module => module.module === FilterPlugin))) || void 0 === _c ? void 0 : _c.moduleOptions);
        plugins.push(filterPlugin);
    }
    if (!(null == disabledPluginsUserSetted ? void 0 : disabledPluginsUserSetted.some((module => module.module === AddRowColumnPlugin)))) {
        const userPluginOptions = null === (_d = null == enabledPluginsUserSetted ? void 0 : enabledPluginsUserSetted.find((module => module.module === AddRowColumnPlugin))) || void 0 === _d ? void 0 : _d.moduleOptions, addRowColumnPlugin = new AddRowColumnPlugin(Object.assign({
            addRowCallback: (row, tableInstance) => {
                tableInstance.addRecord([], row - tableInstance.columnHeaderLevelCount);
            }
        }, userPluginOptions));
        plugins.push(addRowColumnPlugin), enabledPluginsUserSetted = null == enabledPluginsUserSetted ? void 0 : enabledPluginsUserSetted.filter((module => module.module !== AddRowColumnPlugin));
    }
    if (!(null == disabledPluginsUserSetted ? void 0 : disabledPluginsUserSetted.some((module => module.module === TableSeriesNumber)))) {
        const userPluginOptions = null === (_e = null == enabledPluginsUserSetted ? void 0 : enabledPluginsUserSetted.find((module => module.module === TableSeriesNumber))) || void 0 === _e ? void 0 : _e.moduleOptions, tableSeriesNumberPlugin = new TableSeriesNumber(Object.assign({
            rowCount: (null == sheetDefine ? void 0 : sheetDefine.rowCount) || 100,
            colCount: (null == sheetDefine ? void 0 : sheetDefine.columnCount) || 100,
            dragOrder: null == sheetDefine ? void 0 : sheetDefine.dragOrder,
            rowSeriesNumberWidth: 30,
            colSeriesNumberHeight: 30,
            rowSeriesNumberCellStyle: (null === (_f = null == sheetDefine ? void 0 : sheetDefine.theme) || void 0 === _f ? void 0 : _f.rowSeriesNumberCellStyle) || (null === (_g = null == options ? void 0 : options.theme) || void 0 === _g ? void 0 : _g.rowSeriesNumberCellStyle),
            colSeriesNumberCellStyle: (null === (_h = null == sheetDefine ? void 0 : sheetDefine.theme) || void 0 === _h ? void 0 : _h.colSeriesNumberCellStyle) || (null === (_j = null == options ? void 0 : options.theme) || void 0 === _j ? void 0 : _j.colSeriesNumberCellStyle)
        }, userPluginOptions));
        plugins.push(tableSeriesNumberPlugin), enabledPluginsUserSetted = null == enabledPluginsUserSetted ? void 0 : enabledPluginsUserSetted.filter((module => module.module !== TableSeriesNumber));
    }
    if (!(null == disabledPluginsUserSetted ? void 0 : disabledPluginsUserSetted.some((module => module.module === ContextMenuPlugin)))) {
        const contextMenuPlugin = createContextMenuItems(sheetDefine, null === (_k = null == enabledPluginsUserSetted ? void 0 : enabledPluginsUserSetted.find((module => module.module === ContextMenuPlugin))) || void 0 === _k ? void 0 : _k.moduleOptions);
        plugins.push(contextMenuPlugin), enabledPluginsUserSetted = null == enabledPluginsUserSetted ? void 0 : enabledPluginsUserSetted.filter((module => module.module !== ContextMenuPlugin));
    }
    if (!(null == disabledPluginsUserSetted ? void 0 : disabledPluginsUserSetted.some((module => module.module === ExcelEditCellKeyboardPlugin)))) {
        const userPluginOptions = null !== (_m = null === (_l = null == enabledPluginsUserSetted ? void 0 : enabledPluginsUserSetted.find((module => module.module === ExcelEditCellKeyboardPlugin))) || void 0 === _l ? void 0 : _l.moduleOptions) && void 0 !== _m ? _m : {}, excelEditCellKeyboardPlugin = new ExcelEditCellKeyboardPlugin(userPluginOptions);
        plugins.push(excelEditCellKeyboardPlugin), enabledPluginsUserSetted = null == enabledPluginsUserSetted ? void 0 : enabledPluginsUserSetted.filter((module => module.module !== ExcelEditCellKeyboardPlugin));
    }
    if (!(null == disabledPluginsUserSetted ? void 0 : disabledPluginsUserSetted.some((module => module.module === AutoFillPlugin)))) {
        const userPluginOptions = null === (_o = null == enabledPluginsUserSetted ? void 0 : enabledPluginsUserSetted.find((module => module.module === AutoFillPlugin))) || void 0 === _o ? void 0 : _o.moduleOptions, formulaDetectionOptions = createFormulaDetectionOptions(sheetDefine, options, vtableSheet), autoFillPlugin = new AutoFillPlugin(Object.assign(Object.assign({}, userPluginOptions), formulaDetectionOptions));
        plugins.push(autoFillPlugin), enabledPluginsUserSetted = null == enabledPluginsUserSetted ? void 0 : enabledPluginsUserSetted.filter((module => module.module !== AutoFillPlugin));
    }
    return (null == enabledPluginsUserSetted ? void 0 : enabledPluginsUserSetted.length) && enabledPluginsUserSetted.forEach((module => {
        "function" == typeof (null == module ? void 0 : module.module) && plugins.push(new module.module(module.moduleOptions));
    })), plugins;
}

function createFilterPlugin(sheetDefine, userPluginOptions) {
    return new FilterPlugin(Object.assign({
        enableFilter: createColumnFilterChecker(sheetDefine)
    }, userPluginOptions));
}

function createColumnFilterChecker(sheetDefine) {
    return (columnIndex, column) => {
        var _a;
        if (columnIndex < 0 || !sheetDefine.columns || columnIndex >= sheetDefine.columns.length) return !1;
        const columnDefine = sheetDefine.columns[columnIndex];
        return !!(null !== (_a = null == columnDefine ? void 0 : columnDefine.filter) && void 0 !== _a ? _a : sheetDefine.filter);
    };
}

function createContextMenuItems(sheetDefine, userPluginOptions) {
    return new ContextMenuPlugin(Object.assign({
        headerCellMenuItems: [ ...DEFAULT_HEADER_MENU_ITEMS, {
            text: "设置筛选器",
            menuKey: "set_filter"
        }, {
            text: "取消筛选器",
            menuKey: "cancel_filter"
        }, {
            text: "首行表头",
            menuKey: "enable_first_row_as_header"
        }, {
            text: "取消表头",
            menuKey: "disable_first_row_as_header"
        } ],
        bodyCellMenuItems: [ ...DEFAULT_BODY_MENU_ITEMS, {
            text: "启用首行表头",
            menuKey: "enable_first_row_as_header"
        } ],
        columnSeriesNumberMenuItems: [ ...DEFAULT_COLUMN_SERIES_MENU_ITEMS, {
            text: "首行表头",
            menuKey: "enable_first_row_as_header"
        }, {
            text: "取消表头",
            menuKey: "disable_first_row_as_header"
        } ],
        beforeShowAdjustMenuItems: (menuItems, table, col, row) => {
            var _a;
            let isColumnSeriesNumber = !1, isHeaderCell = !1, isBodyCell = !1;
            if (isValid(row) || (isColumnSeriesNumber = !0), isValid(col) && isValid(row) && (table.isHeader(col, row) ? isHeaderCell = !0 : isBodyCell = !0), 
            isHeaderCell) {
                menuItems = (null !== (_a = table.options.columns[col].filter) && void 0 !== _a ? _a : null == sheetDefine ? void 0 : sheetDefine.filter) ? menuItems.filter((item => "string" == typeof item || "set_filter" !== item.menuKey)) : menuItems.filter((item => "string" == typeof item || "cancel_filter" !== item.menuKey));
            }
            return isHeaderCell ? menuItems = menuItems.filter((item => "string" == typeof item || "enable_first_row_as_header" !== item.menuKey)) : isBodyCell ? menuItems = 0 === row ? menuItems.filter((item => "string" == typeof item || "disable_first_row_as_header" !== item.menuKey)) : menuItems.filter((item => "string" == typeof item || "enable_first_row_as_header" !== item.menuKey)) : isColumnSeriesNumber && (menuItems = table.isHeader(col, 0) ? menuItems.filter((item => "string" == typeof item || "enable_first_row_as_header" !== item.menuKey)) : menuItems.filter((item => "string" == typeof item || "disable_first_row_as_header" !== item.menuKey))), 
            menuItems;
        },
        menuClickCallback: {
            set_filter: (args, table) => {
                sheetDefine.columns[args.colIndex].filter = !0;
                const newOptions = Object.assign(Object.assign({}, table.options), {
                    columns: table.options.columns.map(((col, index) => index === args.colIndex ? Object.assign(Object.assign({}, col), {
                        filter: !0
                    }) : col))
                });
                table.updateOption(newOptions, {
                    clearColWidthCache: !1,
                    clearRowHeightCache: !1
                });
            },
            cancel_filter: (args, table) => {
                sheetDefine.columns[args.colIndex].filter = !1;
                const newOptions = Object.assign(Object.assign({}, table.options), {
                    columns: table.options.columns.map(((col, index) => index === args.colIndex ? Object.assign(Object.assign({}, col), {
                        filter: !1
                    }) : col))
                });
                table.updateOption(newOptions, {
                    clearColWidthCache: !1,
                    clearRowHeightCache: !1
                });
            },
            enable_first_row_as_header: (args, table) => {
                handleEnableFirstRowAsHeader(table);
            },
            disable_first_row_as_header: (args, table) => {
                handleDisableFirstRowAsHeader(table);
            }
        }
    }, userPluginOptions));
}

function handleEnableFirstRowAsHeader(table) {
    const firstRecord = table.records[0], new_records = table.records.slice(1), columns = table.columns;
    firstRecord.forEach(((item, index) => {
        columns[index].title = item;
    })), table.updateOption(Object.assign({}, table.options, {
        records: new_records,
        columns: columns,
        showHeader: !0
    }), {
        clearColWidthCache: !1,
        clearRowHeightCache: !1
    }), table.scenegraph.updateNextFrame();
}

function handleDisableFirstRowAsHeader(table) {
    const columns = table.columns, firstRecord = [];
    columns.forEach(((col, index) => {
        firstRecord.push(col.title);
    }));
    const new_records = [ firstRecord, ...table.records ];
    table.updateOption(Object.assign({}, table.options, {
        records: new_records,
        columns: columns,
        showHeader: !1
    }), {
        clearColWidthCache: !1,
        clearRowHeightCache: !1
    }), table.scenegraph.updateNextFrame();
}

function createFormulaDetectionOptions(sheetDefine, options, vtableSheet) {
    return {
        isFormulaCell: (col, row, cellData, table) => {
            var _a;
            if (null == vtableSheet ? void 0 : vtableSheet.formulaManager) try {
                const sheetName = (null === (_a = vtableSheet.sheetManager) || void 0 === _a ? void 0 : _a._activeSheetKey) || "Sheet1";
                return vtableSheet.formulaManager.isCellFormula({
                    sheet: sheetName,
                    row: row,
                    col: col
                });
            } catch (error) {}
            if ("string" == typeof cellData && cellData.startsWith("=")) return !0;
            const cellValue = table.getCellValue(col, row);
            return "string" == typeof cellValue && cellValue.startsWith("=");
        },
        getCellFormula: (col, row, cellData, table) => {
            var _a;
            if (null == vtableSheet ? void 0 : vtableSheet.formulaManager) try {
                const sheetName = (null === (_a = vtableSheet.sheetManager) || void 0 === _a ? void 0 : _a._activeSheetKey) || "Sheet1";
                return vtableSheet.formulaManager.getCellFormula({
                    sheet: sheetName,
                    row: row,
                    col: col
                });
            } catch (error) {}
            const cellValue = table.getCellValue(col, row);
            if ("string" == typeof cellValue && cellValue.startsWith("=")) return cellValue;
        },
        setCellFormula: (col, row, formula, table) => {
            var _a;
            if (null == vtableSheet ? void 0 : vtableSheet.formulaManager) try {
                const sheetName = (null === (_a = vtableSheet.sheetManager) || void 0 === _a ? void 0 : _a._activeSheetKey) || "Sheet1";
                return void vtableSheet.formulaManager.setCellContent({
                    sheet: sheetName,
                    row: row,
                    col: col
                }, formula);
            } catch (error) {}
            table.changeCellValue(col, row, formula);
        }
    };
}
//# sourceMappingURL=table-plugins.js.map