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;
};
import React, { useMemo, useEffect, useCallback } from 'react';
import { CrudTableColumnDefs } from './CrudTableColumnDefs';
import { LookupEntitiesIcon } from '@fluentui/react-icons';
import { useTranslation } from 'react-i18next';
import { useHistory } from 'react-router-dom';
import { LookupTypeIdEnums, UdpEnums } from '../../enums/unitySystemEnums';
import { toTitleCase } from '../../utilities/input/StringUtilities';
import { getSanitizedName } from '../../utilities/entities/entityUtilities';
import { v4 as uuid } from 'uuid';
import { SearchMethodUdpGrid } from '../../UI/templates/newGrid/SearchMethodUdpGrid';
const GRID_ID = '4be3be45-1930-4ab9-b7b7-0c0fa3e30490';
export const CrudBrowserContainer = (props) => {
    var _a;
    const { actionList, onCreate, entityAttributes, foreignKeyData, promotedEntity, apiMethod, filterElements, setAlertWarnings, gridApiCallback, showBackToTableBrowser, gridTitle } = props, other = __rest(props, ["actionList", "onCreate", "entityAttributes", "foreignKeyData", "promotedEntity", "apiMethod", "filterElements", "setAlertWarnings", "gridApiCallback", "showBackToTableBrowser", "gridTitle"]);
    const { t } = useTranslation();
    const history = useHistory();
    const entityName = useMemo(() => {
        return promotedEntity === null || promotedEntity === void 0 ? void 0 : promotedEntity.entityName;
    }, [promotedEntity]);
    const commonEntityName = useMemo(() => {
        if (!entityName)
            return '';
        return toTitleCase(getSanitizedName(entityName));
    }, [entityName]);
    const handleGoToTableBrowser = useCallback(() => {
        history.push({
            pathname: `/tableBrowser`
        });
    }, [history]);
    const secondaryActionButtons = useMemo(() => {
        if (showBackToTableBrowser) {
            return [
                {
                    label: t('Table Browser'),
                    iconName: 'arrowLeft16',
                    onClick: handleGoToTableBrowser
                }
            ];
        }
        else {
            return [];
        }
    }, [handleGoToTableBrowser, showBackToTableBrowser, t]);
    const virtualActions = useMemo(() => {
        return [
            // Action for opening Virtual Configuration
            {
                id: 3,
                title: t('Open Virtual Table'),
                icon: LookupEntitiesIcon,
                loading: !!!foreignKeyData,
                onClick: (value) => {
                    history.push({
                        pathname: `/virtual/${value.id}`,
                        search: `?entityName=${entityName}`,
                        state: {
                            value,
                            promotedEntity,
                            label: value.name
                        }
                    });
                },
                checkDisabled: (value) => value.typeId !== LookupTypeIdEnums.Lookup
            }
        ];
    }, [entityName, foreignKeyData, history, promotedEntity, t]);
    const jointActionList = useMemo(() => {
        if (!actionList || (actionList === null || actionList === void 0 ? void 0 : actionList.length) === 0) {
            return null;
        }
        // Check if it's a udp lookup entity
        if (getSanitizedName(entityName === null || entityName === void 0 ? void 0 : entityName.toLowerCase()) ===
            UdpEnums.Udp_Lookup.toLowerCase()) {
            return [...actionList, ...virtualActions];
        }
        return [...actionList];
    }, [actionList, entityName, virtualActions]);
    const columnDefs = useMemo(() => {
        if (entityAttributes && jointActionList) {
            return CrudTableColumnDefs(entityAttributes, jointActionList);
        }
    }, [entityAttributes, jointActionList]);
    useEffect(() => {
        var _a;
        if (promotedEntity) {
            if (!((_a = promotedEntity === null || promotedEntity === void 0 ? void 0 : promotedEntity.unityBaseSearchMethod) === null || _a === void 0 ? void 0 : _a.apiMethodId)) {
                const searchMethodWarning = {
                    id: uuid(),
                    message: `${promotedEntity.entityName}'s Search method needs to be promoted`
                };
                setAlertWarnings &&
                    setAlertWarnings((prevState) => [...prevState, searchMethodWarning]);
            }
        }
    }, [promotedEntity, setAlertWarnings]);
    const searchMethodConfig = useMemo(() => {
        var _a;
        return {
            apiMethodId: (_a = promotedEntity === null || promotedEntity === void 0 ? void 0 : promotedEntity.unityBaseSearchMethod) === null || _a === void 0 ? void 0 : _a.apiMethodId,
            customSearchObject: { filterElements: filterElements }
        };
    }, [(_a = promotedEntity === null || promotedEntity === void 0 ? void 0 : promotedEntity.unityBaseSearchMethod) === null || _a === void 0 ? void 0 : _a.apiMethodId, filterElements]);
    const gridFunctions = useMemo(() => {
        return [
            { name: 'openSavedViews' },
            { name: 'agGridSizeColumnsToFit' },
            { name: 'agGridAutoSizeColumns' },
            { name: 'agGridResetColumns' },
            { name: 'agGridHideShowColumns' },
            { name: 'agGridRefreshData' }
        ];
    }, []);
    return (React.createElement(SearchMethodUdpGrid, { columnDefs: columnDefs, searchMethodConfig: searchMethodConfig, gridHeaderConfig: {
            title: gridTitle !== null && gridTitle !== void 0 ? gridTitle : commonEntityName,
            primaryActionButton: {
                label: 'Add',
                iconName: 'add',
                onClick: onCreate
            },
            secondaryActionButtons: secondaryActionButtons
        }, gridId: GRID_ID, entityName: commonEntityName, gridOptions: Object.assign({ pagination: true }, other), gridApiCallback: gridApiCallback, gridFunctions: gridFunctions }));
};
//# sourceMappingURL=CrudBrowserContainer.js.map