"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.AdaptableObjectList = exports.AdaptableObjectListItem = exports.AdaptableObjectListItemView = void 0;
const tslib_1 = require("tslib");
const React = tslib_1.__importStar(require("react"));
const react_redux_1 = require("react-redux");
const rebass_1 = require("rebass");
const InternalRedux = tslib_1.__importStar(require("../../../Redux/ActionsReducers/InternalRedux"));
const SimpleButton_1 = tslib_1.__importDefault(require("../../../components/SimpleButton"));
const join_1 = tslib_1.__importDefault(require("../../../components/utils/join"));
const AdaptableHelper_1 = tslib_1.__importDefault(require("../../../Utilities/Helpers/AdaptableHelper"));
const AdaptableContext_1 = require("../../AdaptableContext");
const ButtonDelete_1 = require("../Buttons/ButtonDelete");
const ButtonEdit_1 = require("../Buttons/ButtonEdit");
const ButtonShare_1 = require("../Buttons/ButtonShare");
const SuspendToggleButton_1 = require("../Buttons/SuspendToggleButton/SuspendToggleButton");
const ValueSelector_1 = require("../ValueSelector");
const ICON_SIZE = 26;
const LIST_BASE_CLASS_NAME = 'ab-Adaptable-Object-List';
const ITEM_BASE_CLASS_NAME = `${LIST_BASE_CLASS_NAME}__Item`;
const AdaptableObjectListItemView = (props) => {
    const baseClassName = ITEM_BASE_CLASS_NAME;
    const dispatch = (0, react_redux_1.useDispatch)();
    const deleteActionProps = {
        disabled: props.deleteDisabled,
        tooltip: props.deleteTooltip,
        iconSize: ICON_SIZE,
        ConfirmationMsg: `Are you sure you want to delete this ${props.entityType}?`,
        ConfirmationTitle: `Delete ${props.entityType}`,
        ConfirmAction: props.deleteAction,
        accessLevel: props.accessLevel,
    };
    if (props.disableDeleteConfirmation) {
        deleteActionProps.ConfirmAction = null;
        deleteActionProps.onClickAction = () => {
            dispatch(props.deleteAction);
        };
    }
    const deleteActionButton = React.createElement(ButtonDelete_1.ButtonDelete, { ...deleteActionProps });
    return (React.createElement(rebass_1.Flex, { "data-name": "adaptable-object-list-item", "data-value": props.abObject.Uuid, as: "li", mb: 3, className: (0, join_1.default)(props.className, baseClassName), style: props.style },
        React.createElement(rebass_1.Box, { flex: 1, className: `${baseClassName}__rows` }, props.items.filter?.(Boolean)?.map((tag, index) => {
            const labelElement = typeof tag.label === 'function'
                ? React.createElement(tag.label, { key: index, data: props.abObject })
                : tag.label ?? tag.name;
            return (React.createElement(rebass_1.Flex, { "data-name": tag.name, key: `${index}-${tag.name}`, mb: 2, className: `${baseClassName}__row` },
                !tag.isLabelInline && (React.createElement(rebass_1.Box, { className: `${baseClassName}__label`, mr: 3 },
                    labelElement,
                    props.showEditButton && (React.createElement(SimpleButton_1.default, { "data-name": `${tag.name}-edit-button`, accessLevel: props.accessLevel, className: `${baseClassName}__edit-property`, ml: 1, icon: "edit", tooltip: "edit", iconSize: 18, variant: "text", onClick: () => {
                            props.handleOnEdit(tag.name);
                        } })))),
                React.createElement(rebass_1.Box, { flex: 1, className: `${baseClassName}__values` },
                    typeof tag.view === 'function'
                        ? React.createElement(tag.view, {
                            data: props.abObject,
                            module: props.module,
                        })
                        : tag.view,
                    Boolean(tag?.values && tag?.values?.length) && (React.createElement(ValueSelector_1.ValueOptionsTags, { style: { marginRight: 0 }, readOnly: true, options: tag.values, value: tag.values, allowWrap: true, toIdentifier: (c) => c, toLabel: (c) => React.createElement(React.Fragment, null, c) })),
                    typeof tag.viewAfter === 'function'
                        ? React.createElement(tag.viewAfter, {
                            data: props.abObject,
                            module: props.module,
                        })
                        : tag.viewAfter)));
        })),
        props.showActions && (React.createElement(rebass_1.Flex, { flexDirection: "column", className: `${baseClassName}__buttons` },
            React.createElement(rebass_1.Flex, { justifyContent: "end" },
                props.actions,
                props.teamSharingActivated && (React.createElement(ButtonShare_1.ButtonShare, { iconSize: ICON_SIZE, Header: `TeamSharing ${props.entityType}`, accessLevel: props.accessLevel, onShare: props.onShare })),
                props.onDelete && (React.createElement(SimpleButton_1.default, { "data-name": "delete", disabled: props.deleteDisabled, iconSize: ICON_SIZE, tooltip: props.deleteTooltip, icon: "delete", onClick: props.onDelete, variant: "text" })),
                props.deleteAction && deleteActionButton,
                props.showEditButton && (React.createElement(ButtonEdit_1.ButtonEdit, { iconSize: ICON_SIZE, disabled: props.editDisabled, accessLevel: props.accessLevel, onClick: () => props.handleOnEdit() }))),
            React.createElement(rebass_1.Box, { flex: 1 }),
            props.suspendedEnabled && (React.createElement(rebass_1.Flex, { justifyContent: "end" },
                React.createElement(SuspendToggleButton_1.SuspendToggleButton, { onSuspend: props.onSuspend, onUnSuspend: props.onUnSuspend, suspendableObject: props.abObject, accessLevel: props.accessLevel })))))));
};
exports.AdaptableObjectListItemView = AdaptableObjectListItemView;
const AdaptableObjectListItem = (props) => {
    const adaptable = (0, AdaptableContext_1.useAdaptable)();
    const dispatch = (0, react_redux_1.useDispatch)();
    const [isEditWizardVisible, setIsEditWizardVisible] = React.useState(false);
    const [wizardStepName, setWizardStepName] = React.useState(null);
    const viewOptions = props.module?.getViewProperties?.() ?? {};
    const EditWizard = viewOptions.getEditWizard?.(props.data.abObject);
    const deleteAction = viewOptions?.getDeleteAction?.(props.data.abObject);
    const isObjectShareable = props.module.isModuleObjectsShareable();
    const teamSharingActivated = isObjectShareable &&
        adaptable.api.teamSharingApi.isTeamSharingAvailable() &&
        adaptable.api.teamSharingApi.hasTeamSharingFullRights();
    const entityType = props.module.moduleInfo.FriendlyName;
    const moduleAccessLevel = adaptable.api.entitlementApi.getEntitlementAccessLevelForModule(props.module.moduleInfo.ModuleName);
    const accessLevel = AdaptableHelper_1.default.getAccessLevelForObject(props.data.abObject, moduleAccessLevel);
    const itemClassName = (0, join_1.default)(props.data.className, ITEM_BASE_CLASS_NAME, props.data.abObject.IsSuspended &&
        `${ITEM_BASE_CLASS_NAME}--is-suspended`);
    const handleCloseWizard = React.useCallback(() => {
        setIsEditWizardVisible(false);
        setWizardStepName(null);
    }, []);
    const handleOnEdit = React.useCallback((sectionName) => {
        if (EditWizard) {
            setIsEditWizardVisible(true);
            if (sectionName) {
                setWizardStepName(sectionName);
            }
        }
        viewOptions?.onOpenEditPopup?.(props.data.abObject);
    }, []);
    const hasSuspend = Boolean(viewOptions.getSuspendAction);
    const actions = viewOptions?.actions?.map?.((component, index) => {
        return React.createElement(component, {
            data: props.data.abObject,
            key: index,
            accessLevel,
        });
    });
    const isEditDisabled = !Boolean(EditWizard || viewOptions.onOpenEditPopup);
    const showActions = !props.hideControls;
    const showEditButton = Boolean(EditWizard);
    const adaptableOpttions = adaptable.api.optionsApi.getAdaptableOptions();
    const disableDeleteConfirmationState = (0, react_redux_1.useSelector)((adaptableState) => InternalRedux.DisableDeleteConfirmationSelector(adaptableState.Internal));
    const disableDeleteConfirmation = disableDeleteConfirmationState ||
        adaptableOpttions?.userInterfaceOptions?.disableDeleteConfirmation;
    return (React.createElement(React.Fragment, null,
        React.createElement(exports.AdaptableObjectListItemView, { module: props.module, disableDeleteConfirmation: disableDeleteConfirmation, abObject: props.data.abObject, accessLevel: accessLevel, actions: actions, className: itemClassName, handleOnEdit: handleOnEdit, items: props.data.items, showActions: showActions, showEditButton: showEditButton, style: props.data.style, teamSharingActivated: teamSharingActivated, onShare: (config) => adaptable.api.teamSharingApi.shareAdaptableEntity(props.data.abObject, props.module.moduleInfo.ModuleName, config), entityType: entityType, deleteAction: deleteAction, deleteDisabled: props.deleteDisabled, deleteTooltip: props.deleteTooltip, editDisabled: isEditDisabled, suspendedEnabled: hasSuspend, onSuspend: () => dispatch(viewOptions.getSuspendAction(props.data.abObject)), onUnSuspend: () => dispatch(viewOptions.getUnSuspendAction(props.data.abObject)) }),
        isEditWizardVisible && EditWizard && (React.createElement(EditWizard, { defaultCurrentSectionName: wizardStepName, moduleInfo: props.module.moduleInfo, data: props.data.abObject, configEntities: null, onCloseWizard: handleCloseWizard, onFinishWizard: handleCloseWizard }))));
};
exports.AdaptableObjectListItem = AdaptableObjectListItem;
const AdaptableObjectList = (props) => {
    return (React.createElement("ul", { className: LIST_BASE_CLASS_NAME }, props?.items?.map((item, index) => (React.createElement(exports.AdaptableObjectListItem, { key: item.abObject.Uuid ?? index, data: item, module: props.module, ...props.itemProps })))));
};
exports.AdaptableObjectList = AdaptableObjectList;
