import { GridStateTypeEnums } from '../../enums/unitySystemEnums.ts';
export const useGridFormat = () => {
    const gridFormat = (gridApi, type) => {
        type === 'expand' && gridApi.expandAll();
        type === 'collapse' && gridApi.collapseAll();
        type === 'resize' && gridApi.sizeColumnsToFit();
    };
    return [gridFormat];
};
export const useGridClear = () => {
    const gridClear = (gridApi, gridColumnApi) => {
        gridColumnApi.setRowGroupColumns([]);
        gridApi.setFilterModel(null);
        gridApi.setQuickFilter('');
        gridApi.sizeColumnsToFit();
        gridColumnApi.resetColumnState();
    };
    return [gridClear];
};
export const useQuickFilter = () => {
    const quickFilter = (gridApi, event) => {
        gridApi.setQuickFilter(event === null || event === void 0 ? void 0 : event.target.value);
    };
    return [quickFilter];
};
export const useExport = () => {
    const gridExport = (gridApi, type) => {
        type === 'excel' && gridApi.exportDataAsExcel();
        type === 'csv' && gridApi.exportDataAsCsv();
    };
    return [gridExport];
};
export const saveGridState = async (gridId, gridApi, gridColumnApi, execute) => {
    const columnState = gridColumnApi === null || gridColumnApi === void 0 ? void 0 : gridColumnApi.getColumnState();
    const columnGroupState = gridColumnApi === null || gridColumnApi === void 0 ? void 0 : gridColumnApi.getColumnGroupState();
    const sortModel = gridApi.getSortModel();
    const filterModel = gridApi.getFilterModel();
    const isPivotMode = gridColumnApi.isPivotMode();
    const gridState = [];
    const columnGridConfiguration = {
        gridStateTypeId: GridStateTypeEnums.Column,
        gridId: gridId,
        values: JSON.stringify(columnState),
    };
    const columnGroupGridConfiguration = {
        gridStateTypeId: GridStateTypeEnums.Column_Group,
        gridId: gridId,
        values: JSON.stringify(columnGroupState),
    };
    const columnSortGridConfiguration = {
        gridStateTypeId: GridStateTypeEnums.Column_Sort,
        gridId: gridId,
        values: JSON.stringify(sortModel),
    };
    const columnFilterGridConfiguration = {
        gridStateTypeId: GridStateTypeEnums.Column_Filter,
        gridId: gridId,
        values: JSON.stringify(filterModel),
    };
    const pivotGridConfiguration = {
        gridStateTypeId: GridStateTypeEnums.Pivot_Mode,
        gridId: gridId,
        values: JSON.stringify(isPivotMode),
    };
    gridState.push(columnGridConfiguration);
    gridState.push(columnGroupGridConfiguration);
    gridState.push(columnSortGridConfiguration);
    gridState.push(columnFilterGridConfiguration);
    gridState.push(pivotGridConfiguration);
    return await execute({ data: gridState });
};
export const getGridState = (gridApi, gridColumnApi, gridId) => {
    const columnState = gridColumnApi === null || gridColumnApi === void 0 ? void 0 : gridColumnApi.getColumnState();
    const columnGroupState = gridColumnApi === null || gridColumnApi === void 0 ? void 0 : gridColumnApi.getColumnGroupState();
    const sortModel = gridApi.getSortModel();
    const filterModel = gridApi.getFilterModel();
    const isPivotMode = gridColumnApi.isPivotMode();
    const gridState = [];
    const columnGridConfiguration = {
        gridStateTypeId: GridStateTypeEnums.Column,
        gridId: gridId,
        values: JSON.stringify(columnState),
    };
    const columnGroupGridConfiguration = {
        gridStateTypeId: GridStateTypeEnums.Column_Group,
        gridId: gridId,
        values: JSON.stringify(columnGroupState),
    };
    const columnSortGridConfiguration = {
        gridStateTypeId: GridStateTypeEnums.Column_Sort,
        gridId: gridId,
        values: JSON.stringify(sortModel),
    };
    const columnFilterGridConfiguration = {
        gridStateTypeId: GridStateTypeEnums.Column_Filter,
        gridId: gridId,
        values: JSON.stringify(filterModel),
    };
    const pivotGridConfiguration = {
        gridStateTypeId: GridStateTypeEnums.Pivot_Mode,
        gridId: gridId,
        values: JSON.stringify(isPivotMode),
    };
    gridState.push(columnGridConfiguration);
    gridState.push(columnGroupGridConfiguration);
    gridState.push(columnSortGridConfiguration);
    gridState.push(columnFilterGridConfiguration);
    gridState.push(pivotGridConfiguration);
    return gridState;
};
export const deleteGridState = async (gridApi, gridColumnApi, deleteGridState) => {
    clearGridState(gridApi, gridColumnApi);
    return await deleteGridState();
};
export const clearGridState = (gridApi, gridColumnApi) => {
    gridColumnApi.resetColumnState();
    gridColumnApi.resetColumnGroupState();
    gridApi.setFilterModel({});
    gridApi.setSortModel([]);
    gridColumnApi.setPivotMode(false);
    gridApi.sizeColumnsToFit();
};
export const onFirstDataRendered = (gridApi, gridColumnApi, gridStates) => {
    if (gridStates && gridStates.length) {
        gridStates.forEach(state => {
            switch (state.gridStateTypeId) {
                case GridStateTypeEnums.Column:
                    gridColumnApi.setColumnState(JSON.parse(state.values));
                    break;
                case GridStateTypeEnums.Column_Group:
                    gridColumnApi.setColumnGroupState(JSON.parse(state.values));
                    break;
                case GridStateTypeEnums.Pivot_Mode:
                    gridColumnApi.setPivotMode(JSON.parse(state.values));
                    break;
                case GridStateTypeEnums.Column_Sort:
                    gridApi.setSortModel(JSON.parse(state.values));
                    break;
                case GridStateTypeEnums.Column_Filter:
                    gridApi.setFilterModel(JSON.parse(state.values));
                    break;
                default:
                    break;
            }
        });
    }
};
//# sourceMappingURL=GridFunctions.js.map