import { makeStyles, Typography } from '@material-ui/core';
import React, { useCallback, useEffect, useMemo, useState } from 'react';
import { getPathObject, getPrimaryKeyValues } from '../../utilities/maintenanceEngine/CrudBrowserUtilities';
import { toLowerFirstChar, toTitleCase } from '../../utilities/input/StringUtilities';
import { useSnackbar } from 'notistack';
import { useTranslation } from 'react-i18next';
import { useAxiosGet } from '../../utilities/useAxiosGet';
import { ConfigService } from '../../configService';
const useStyles = makeStyles((theme) => ({
    dataRoot: {
        marginTop: theme.spacing(2)
    }
}));
export const EntitySideBarContent = (props) => {
    const { action, executeAction, entityAttributes, selectedNodeObj, apiCatalogId, entityName, sidebarEntity, menuNodeList, closeSplitScreen } = props;
    const classes = useStyles(props);
    const [data, setData] = useState(null);
    const { enqueueSnackbar } = useSnackbar();
    const { t } = useTranslation();
    const [{ data: selectedEntity }] = useAxiosGet(ConfigService.integrationV1ApiUrl, `apicatalog/${apiCatalogId}/promotedMethodEntities/${entityName}/product/${ConfigService.config.UNITY_PRODUCT_ID}`, {}, !!!apiCatalogId || !!!entityName || !!sidebarEntity, false);
    const entity = useMemo(() => {
        return selectedEntity || sidebarEntity;
    }, [selectedEntity, sidebarEntity]);
    const showError = useCallback(() => {
        enqueueSnackbar(t(`Invalid action value.`), {
            variant: 'error',
            anchorOrigin: {
                vertical: 'top',
                horizontal: 'center'
            }
        });
    }, [enqueueSnackbar, t]);
    const onGet = useCallback(async (data) => {
        if ((entity === null || entity === void 0 ? void 0 : entity.unityBaseGetMethod) && (action === null || action === void 0 ? void 0 : action.value)) {
            if (entity.unityBaseGetMethod.apiMethodId === action.value) {
                return await executeAction(data, action.value);
            }
            else {
                showError();
            }
        }
    }, [action, executeAction, entity, showError]);
    const handleOnGet = useCallback(async (value) => {
        var _a;
        if (entity === null || entity === void 0 ? void 0 : entity.unityBaseGetMethod) {
            // Get primary key value object using path parameters and merge with data
            const primaryKeyValues = getPrimaryKeyValues(entityAttributes, value);
            const pathObject = getPathObject((_a = entity === null || entity === void 0 ? void 0 : entity.unityBaseGetMethod) === null || _a === void 0 ? void 0 : _a.pathParameters, primaryKeyValues);
            const data = Object.assign({}, pathObject);
            const response = await onGet({ data }).catch((res) => {
                enqueueSnackbar(res.message, {
                    variant: 'error',
                    anchorOrigin: {
                        vertical: 'top',
                        horizontal: 'center'
                    }
                });
            });
            if ((response === null || response === void 0 ? void 0 : response.status) === 200) {
                setData(response.data);
            }
        }
    }, [enqueueSnackbar, entityAttributes, onGet, entity]);
    const renderData = useCallback((schemaAttribute) => {
        var _a;
        if (!data || ((_a = schemaAttribute === null || schemaAttribute === void 0 ? void 0 : schemaAttribute.extendedProperties) === null || _a === void 0 ? void 0 : _a.childEntity)) {
            return null;
        }
        return (React.createElement("div", { className: classes.dataRoot }, React.createElement(Typography, { variant: "subtitle2", style: { marginTop: 8 } }, toTitleCase(schemaAttribute === null || schemaAttribute === void 0 ? void 0 : schemaAttribute.name)), React.createElement(Typography, null, data[toLowerFirstChar(schemaAttribute.name)])));
    }, [classes, data]);
    const cardContent = useMemo(() => {
        return (React.createElement("div", null, entityAttributes === null || entityAttributes === void 0 ? void 0 : entityAttributes.map((schemaAttribute, index) => {
            var _a;
            return (!((_a = schemaAttribute === null || schemaAttribute === void 0 ? void 0 : schemaAttribute.extendedProperties) === null || _a === void 0 ? void 0 : _a.identifier) && (React.createElement(React.Fragment, { key: `${schemaAttribute.name}-${index}` }, renderData(schemaAttribute))));
        })));
    }, [entityAttributes, renderData]);
    useEffect(() => {
        if (!data) {
            handleOnGet(selectedNodeObj);
        }
    }, [handleOnGet, selectedNodeObj, data]);
    return React.createElement("div", null, cardContent);
};
//# sourceMappingURL=EntitySideBarContent.js.map