"use strict";
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);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.useTableColumns = void 0;
var ramda_1 = require("ramda");
var react_1 = require("react");
var mdm_sdk_1 = require("@reltio/mdm-sdk");
var useTableColumns = function (_a) {
    var defaultColumns = _a.defaultColumns, columns = _a.columns, allColumnsData = _a.allColumnsData, onChangeColumns = _a.onChangeColumns;
    var _b = (0, react_1.useState)(), sorting = _b[0], setSorting = _b[1];
    var _c = (0, react_1.useState)([]), visibleColumnsData = _c[0], setVisibleColumnsData = _c[1];
    var visibleColumnIds = (0, react_1.useMemo)(function () { return visibleColumnsData.map(function (_a) {
        var id = _a.id;
        return id;
    }); }, [visibleColumnsData]);
    (0, react_1.useEffect)(function () {
        var allColumns = (0, ramda_1.isEmpty)(columns) ? defaultColumns.map(function (id) { return ({ id: id, size: undefined }); }) : columns;
        var newVisibleColumnsData = allColumns.reduce(function (columnsData, _a, index) {
            var id = _a.id, size = _a.size;
            var columnData = allColumnsData.find((0, ramda_1.propEq)('id', id));
            if (columnData) {
                columnsData.push(__assign(__assign(__assign({}, columnData), { autoResize: size ? false : index !== 0 }), (size ? { size: size } : {})));
            }
            return columnsData;
        }, []);
        setVisibleColumnsData(newVisibleColumnsData);
    }, [columns, allColumnsData, defaultColumns]);
    var handleChangeColumns = (0, react_1.useCallback)(function (newColumns) {
        onChangeColumns(newColumns.map(function (column) {
            var visibleColumnData = visibleColumnsData.find((0, ramda_1.propEq)('id', column));
            var size = visibleColumnData === null || visibleColumnData === void 0 ? void 0 : visibleColumnData.size;
            var autoResize = visibleColumnData === null || visibleColumnData === void 0 ? void 0 : visibleColumnData.autoResize;
            return __assign({ id: column }, (size && !autoResize ? { size: size } : {}));
        }));
    }, [onChangeColumns, visibleColumnsData]);
    var handleReorderingColumn = (0, react_1.useCallback)(function (oldPositionId, newPositionId) {
        var oldPosition = columns.map(function (_a) {
            var id = _a.id;
            return id;
        }).indexOf(oldPositionId);
        var newPosition = columns.map(function (_a) {
            var id = _a.id;
            return id;
        }).indexOf(newPositionId);
        handleChangeColumns((0, ramda_1.move)(oldPosition, newPosition, columns.map(function (_a) {
            var id = _a.id;
            return id;
        })));
    }, [columns, handleChangeColumns]);
    var handleChangeColumnsSize = (0, react_1.useCallback)(function (newColumnsSize) {
        var oldColumnsSize = (0, ramda_1.project)(['id', 'size'], visibleColumnsData);
        if ((0, ramda_1.symmetricDifference)(newColumnsSize, oldColumnsSize).length > 0) {
            var newVisibleColumnsData = visibleColumnsData.map(function (column) {
                var columnSize = newColumnsSize.find((0, ramda_1.propEq)('id', column.id));
                return columnSize ? __assign(__assign({}, column), { size: columnSize.size }) : column;
            });
            setVisibleColumnsData(newVisibleColumnsData);
        }
        if (newColumnsSize.some((0, ramda_1.propEq)('manuallyChanged', true))) {
            if ((0, ramda_1.isEmpty)(columns)) {
                onChangeColumns(newColumnsSize.map(function (_a) {
                    var id = _a.id, size = _a.size, manuallyChanged = _a.manuallyChanged;
                    return (__assign({ id: id }, (manuallyChanged ? { size: size } : {})));
                }));
            }
            else {
                onChangeColumns(columns.map(function (column) {
                    var columnSize = newColumnsSize.find((0, ramda_1.propEq)('id', column.id));
                    return (columnSize === null || columnSize === void 0 ? void 0 : columnSize.manuallyChanged) ? __assign(__assign({}, column), { size: columnSize.size }) : column;
                }));
            }
        }
    }, [columns, onChangeColumns, visibleColumnsData]);
    var handleSorting = (0, react_1.useCallback)(function (field) {
        setSorting({
            field: field,
            order: (sorting === null || sorting === void 0 ? void 0 : sorting.field) === field ? (0, mdm_sdk_1.toggleSortOrder)(sorting.order) : mdm_sdk_1.SortOrder.asc
        });
    }, [sorting === null || sorting === void 0 ? void 0 : sorting.field, sorting === null || sorting === void 0 ? void 0 : sorting.order]);
    return {
        handleChangeColumns: handleChangeColumns,
        handleReorderingColumn: handleReorderingColumn,
        handleSorting: handleSorting,
        visibleColumnIds: visibleColumnIds,
        visibleColumnsData: visibleColumnsData,
        sorting: sorting,
        handleChangeColumnsSize: handleChangeColumnsSize
    };
};
exports.useTableColumns = useTableColumns;
