var __assign = (this && this.__assign) || function () {
    __assign = Object.assign || function(t) {
        for (var s, i = 1, n = arguments.length; i < n; i++) {
            s = arguments[i];
            for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
                t[p] = s[p];
        }
        return t;
    };
    return __assign.apply(this, arguments);
};
import { toggleSortOrder, createStandardAction } from '@reltio/mdm-sdk';
import { always, assoc, difference, dissoc, evolve, keys, path, when } from 'ramda';
export var ActionTypes;
(function (ActionTypes) {
    ActionTypes["CHANGE_COLUMNS"] = "CHANGE_COLUMNS";
    ActionTypes["TOGGLE_FILTERS"] = "TOGGLE_FILTERS";
    ActionTypes["CHANGE_FILTER"] = "CHANGE_FILTER";
    ActionTypes["TOGGLE_SORT"] = "TOGGLE_SORT";
    ActionTypes["CHANGE_PAGE"] = "CHANGE_PAGE";
    ActionTypes["CHANGE_ROWS_PER_PAGE"] = "CHANGE_ROWS_PER_PAGE";
})(ActionTypes || (ActionTypes = {}));
export var actions = {
    changeColumns: createStandardAction(ActionTypes.CHANGE_COLUMNS),
    toggleFilters: createStandardAction(ActionTypes.TOGGLE_FILTERS),
    changeFilter: createStandardAction(ActionTypes.CHANGE_FILTER),
    toggleSort: createStandardAction(ActionTypes.TOGGLE_SORT),
    changePage: createStandardAction(ActionTypes.CHANGE_PAGE),
    changeRowsPerPage: createStandardAction(ActionTypes.CHANGE_ROWS_PER_PAGE)
};
export var changeVisibleColumns = function (state, nextVisibleColumns) {
    if (nextVisibleColumns === void 0) { nextVisibleColumns = []; }
    var removedColumns = difference(state.visibleColumns, nextVisibleColumns);
    var isRemovedColumn = function (columnId) { return removedColumns.includes(columnId); };
    return evolve({
        visibleColumns: always(nextVisibleColumns),
        columnsSize: function (columnsSize) {
            if (columnsSize === void 0) { columnsSize = []; }
            return nextVisibleColumns.map(function (columnId) {
                var columnSize = columnsSize.find(function (_a) {
                    var id = _a.id;
                    return id === columnId;
                });
                return __assign({ id: columnId, size: (columnSize === null || columnSize === void 0 ? void 0 : columnSize.manuallyChanged) ? columnSize.size : 0 }, ((columnSize === null || columnSize === void 0 ? void 0 : columnSize.manuallyChanged) ? { manuallyChanged: true } : {}));
            });
        },
        sorting: when(function (sorting) {
            if (sorting === void 0) { sorting = {}; }
            return isRemovedColumn(sorting.field);
        }, function () {
            var nextSortField = nextVisibleColumns[0];
            return { field: nextSortField, order: 'asc' };
        }),
        filters: function (filters) {
            if (filters === void 0) { filters = {}; }
            return keys(filters).reduce(function (acc, columnId) {
                return isRemovedColumn(columnId) ? dissoc(columnId, acc) : acc;
            }, filters);
        }
    }, state);
};
export var reducer = function (state, action) {
    switch (action.type) {
        case ActionTypes.CHANGE_COLUMNS: {
            var columnIds = action.payload;
            return changeVisibleColumns(state, columnIds);
        }
        case ActionTypes.TOGGLE_FILTERS: {
            var enableFilters = !state.filters;
            return __assign(__assign({}, state), { filters: enableFilters ? {} : null, page: enableFilters ? state.page : 0 });
        }
        case ActionTypes.CHANGE_FILTER: {
            var _a = action.payload, columnId = _a.columnId, filter = _a.filter;
            return evolve({ filters: filter ? assoc(columnId, filter) : dissoc(columnId), page: always(0) }, state);
        }
        case ActionTypes.TOGGLE_SORT: {
            var nextSortField = action.payload;
            var prevSortField = path(['sorting', 'field'], state);
            var prevSortOrder = path(['sorting', 'order'], state);
            return __assign(__assign({}, state), { sorting: {
                    field: nextSortField,
                    order: toggleSortOrder(nextSortField === prevSortField ? prevSortOrder : null)
                }, page: 0 });
        }
        case ActionTypes.CHANGE_PAGE:
            return __assign(__assign({}, state), { page: action.payload });
        case ActionTypes.CHANGE_ROWS_PER_PAGE:
            return __assign(__assign({}, state), { rowsPerPage: action.payload });
        default:
            return state;
    }
};
export default {
    reducer: reducer,
    actions: actions,
    ActionTypes: ActionTypes,
    changeVisibleColumns: changeVisibleColumns
};
