"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);
};
var __rest = (this && this.__rest) || function (s, e) {
    var t = {};
    for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
        t[p] = s[p];
    if (s != null && typeof Object.getOwnPropertySymbols === "function")
        for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
            if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
                t[p[i]] = s[p[i]];
        }
    return t;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.buildColumnsSizeById = exports.buildColumnsFilter = exports.columnFilterToMdmFilter = exports.defaultRenderRowCell = exports.defaultGetRowCellHeight = void 0;
var react_1 = __importDefault(require("react"));
var ramda_1 = require("ramda");
var mdm_sdk_1 = require("@reltio/mdm-sdk");
var BLOB_HEIGHT = 100;
var IMAGE_HEIGHT = 64;
var PADDING = 15;
var defaultGetRowCellHeight = function (_a) {
    var _b;
    var columnData = _a.columnData, cell = _a.cell;
    var valuesCount = cell.values.length;
    switch ((_b = columnData === null || columnData === void 0 ? void 0 : columnData.dataTypeDefinition) === null || _b === void 0 ? void 0 : _b.type) {
        case mdm_sdk_1.DataTypes.TYPE_BLOB:
            return BLOB_HEIGHT * valuesCount;
        case mdm_sdk_1.DataTypes.TYPE_IMAGE:
            return (PADDING + IMAGE_HEIGHT) * valuesCount + PADDING;
        case mdm_sdk_1.DataTypes.TYPE_REFERENCE:
            return 28 * Math.max(valuesCount, 1) + PADDING;
        default:
            return 33 * Math.max(valuesCount, 1) + PADDING;
    }
};
exports.defaultGetRowCellHeight = defaultGetRowCellHeight;
var defaultRenderRowCell = function (_a) {
    var cell = _a.cell, CellValueRenderer = _a.CellValueRenderer, otherProps = __rest(_a, ["cell", "CellValueRenderer"]);
    return cell.values.map(function (value, index) { return react_1.default.createElement(CellValueRenderer, __assign({ key: index, value: value }, otherProps)); });
};
exports.defaultRenderRowCell = defaultRenderRowCell;
var columnFilterToMdmFilter = function (columnData, columnFilter) {
    if (columnFilter === void 0) { columnFilter = {}; }
    return ({
        fieldName: columnData.id,
        filter: columnFilter.filter,
        values: [columnFilter.value],
        type: columnData.dataTypeDefinition.type
    });
};
exports.columnFilterToMdmFilter = columnFilterToMdmFilter;
var buildColumnsFilter = function (columnsData, filters) {
    return mdm_sdk_1.Maybe.fromNull(filters)
        .map(function (filters) {
        return Object.entries(filters)
            .map(function (_a) {
            var columnId = _a[0], columnFilter = _a[1];
            var columnData = columnsData.find((0, ramda_1.propEq)('id', columnId));
            return columnData && (0, exports.columnFilterToMdmFilter)(columnData, columnFilter);
        })
            .filter(ramda_1.identity);
    })
        .map((0, mdm_sdk_1.buildFilterQueryString)())
        .orSome('');
};
exports.buildColumnsFilter = buildColumnsFilter;
var buildColumnsSizeById = function (columnsSize) {
    if (columnsSize === void 0) { columnsSize = []; }
    return columnsSize.reduce(function (columnsSizeById, _a) {
        var id = _a.id, size = _a.size, manuallyChanged = _a.manuallyChanged;
        if (manuallyChanged) {
            columnsSizeById[id] = size;
        }
        return columnsSizeById;
    }, {});
};
exports.buildColumnsSizeById = buildColumnsSizeById;
