import { toTitleCase, toLowerFirstChar } from '../utilities/input/StringUtilities';
const defaultGridColumn = {
    headerName: '',
    field: '',
    minWidth: 80,
    autoHeight: false,
    sortable: false
};
export const DataBrowserColumnDefs = (entityAttributes, actionList, isServerSide, queryColumnsData, columnName = 'Actions', crudTableActionRenderName = 'actionsRenderer', panelDirection = 'right') => {
    const childEntityAttributes = entityAttributes === null || entityAttributes === void 0 ? void 0 : entityAttributes.reduce((defs, element) => {
        var _a;
        if ((_a = element === null || element === void 0 ? void 0 : element.extendedProperties) === null || _a === void 0 ? void 0 : _a.childEntity) {
            defs.push(element);
        }
        return defs;
    }, []);
    let shownColumns = [];
    let hiddenColumns = [];
    entityAttributes === null || entityAttributes === void 0 ? void 0 : entityAttributes.forEach((element) => {
        var _a, _b, _c;
        if (!((_a = element === null || element === void 0 ? void 0 : element.extendedProperties) === null || _a === void 0 ? void 0 : _a.childEntity)) {
            let matchingColumn = null;
            const matchingColumnIndex = queryColumnsData.findIndex((column) => column.name === (element === null || element === void 0 ? void 0 : element.name));
            if (matchingColumnIndex >= 0) {
                matchingColumn = queryColumnsData[matchingColumnIndex];
            }
            // update column header if attribute has a child entity
            const childEntity = childEntityAttributes.find((ea) => {
                var _a, _b, _c, _d;
                return Object.keys(((_a = ea === null || ea === void 0 ? void 0 : ea.extendedProperties) === null || _a === void 0 ? void 0 : _a.childEntityAssoc) || {}).length &&
                    !((_c = (_b = ea === null || ea === void 0 ? void 0 : ea.extendedProperties) === null || _b === void 0 ? void 0 : _b.childEntityAssoc) === null || _c === void 0 ? void 0 : _c.UdpType) &&
                    (ea === null || ea === void 0 ? void 0 : ea.name) === (element === null || element === void 0 ? void 0 : element.name) &&
                    ((_d = ea === null || ea === void 0 ? void 0 : ea.extendedProperties) === null || _d === void 0 ? void 0 : _d.childEntityAssoc.Keys.find((k) => toLowerFirstChar(k).includes(element === null || element === void 0 ? void 0 : element.name)));
            });
            let columnHeaderName = toTitleCase(childEntity ? childEntity === null || childEntity === void 0 ? void 0 : childEntity.reference : element === null || element === void 0 ? void 0 : element.name);
            if (matchingColumn) {
                columnHeaderName = toTitleCase(childEntity
                    ? childEntity === null || childEntity === void 0 ? void 0 : childEntity.reference
                    : (matchingColumn === null || matchingColumn === void 0 ? void 0 : matchingColumn.alias) || (matchingColumn === null || matchingColumn === void 0 ? void 0 : matchingColumn.name));
            }
            let def = Object.assign(Object.assign({}, defaultGridColumn), { headerName: columnHeaderName, field: (element === null || element === void 0 ? void 0 : element.name.charAt(0).toLowerCase()) + (element === null || element === void 0 ? void 0 : element.name.slice(1)), hide: !matchingColumn });
            if (((_b = element === null || element === void 0 ? void 0 : element.extendedProperties) === null || _b === void 0 ? void 0 : _b.searchable) && isServerSide) {
                def = Object.assign(Object.assign({}, def), { filter: 'agTextColumnFilter', filterParams: {
                        suppressAndOrCondition: true,
                        filterOptions: [
                            'equals',
                            'contains',
                            'notEqual',
                            'greaterThan',
                            'lessThan',
                            'greaterThanOrEqual',
                            'lessThanOrEqual'
                        ]
                    }, enableRowGroup: true });
            }
            if (((_c = element === null || element === void 0 ? void 0 : element.extendedProperties) === null || _c === void 0 ? void 0 : _c.orderable) || !isServerSide) {
                def = Object.assign(Object.assign({}, def), { sortable: true });
            }
            if (matchingColumn) {
                shownColumns[matchingColumnIndex] = def;
            }
            else {
                hiddenColumns.push(def);
            }
        }
    });
    let columnDefs = shownColumns.concat(hiddenColumns);
    columnDefs.push({
        headerName: crudTableActionRenderName === 'actionsRenderer' ? 'Actions' : columnName,
        filter: false,
        pinned: panelDirection,
        width: 150,
        maxWidth: 180,
        cellRenderer: crudTableActionRenderName,
        cellRendererParams: {
            actionList
        },
        valueGetter: (params) => params.data,
        suppressMenu: true,
        sortable: false,
        cellStyle: { display: 'flex', alignItems: 'center' }
    });
    return columnDefs;
};
export const defaultColActionsDef = {
    resizable: true,
    editable: false,
    sortable: false,
    filter: true,
    floatingFilter: false,
    enableRowGroup: true
};
//# sourceMappingURL=DataBrowserColumnDefs.js.map