"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.AdaptableObjectCompactList = exports.AdaptableObjectCompactListItem = 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 SimpleButton_1 = tslib_1.__importDefault(require("../../../components/SimpleButton"));
const AdaptableContext_1 = require("../../AdaptableContext");
const ValueSelector_1 = require("../ValueSelector");
const baseClassName = 'ab-Adaptable-Object-Compact-List';
const ICON_SIZE = 20;
const AdaptableObjectCompactListItem = (props) => {
    const dispatch = (0, react_redux_1.useDispatch)();
    const adaptable = (0, AdaptableContext_1.useAdaptable)();
    const objectView = props.module.toViewCompact(props.abObject);
    const viewOptions = props.module?.getViewProperties?.();
    const deleteAction = viewOptions?.getCompactDeleteAction?.(props.abObject);
    const moduleAccessLevel = adaptable.api.entitlementApi.getEntitlementAccessLevelForModule(props.module.moduleInfo.ModuleName);
    const suspendAction = viewOptions?.getCompactSuspendAction?.(props.abObject);
    const unSuspendAction = viewOptions?.getCompactUnSuspendAction?.(props.abObject);
    const isSuspended = props.abObject.IsSuspended;
    const handleDelete = React.useCallback((event) => {
        event.stopPropagation();
        event.preventDefault();
        dispatch(deleteAction);
    }, []);
    const handleSuspendUnSuspend = React.useCallback((event) => {
        event.stopPropagation();
        event.preventDefault();
        if (isSuspended) {
            dispatch(unSuspendAction);
        }
        else {
            dispatch(suspendAction);
        }
    }, [props.abObject]);
    let labelEl = objectView.item.label;
    if (typeof labelEl === 'function') {
        labelEl = React.createElement(labelEl, { data: props.abObject });
    }
    labelEl = labelEl ?? objectView.item.name;
    return (React.createElement(rebass_1.Flex, { className: `${baseClassName}__Item`, alignItems: "center" },
        labelEl && (React.createElement(rebass_1.Flex, { flex: '1 1 auto', className: `${baseClassName}__Item__Name` }, labelEl)),
        React.createElement(rebass_1.Box, { flex: '2 1 auto' },
            typeof objectView.item.view === 'function'
                ? React.createElement(objectView.item.view, {
                    data: props.abObject,
                })
                : objectView.item.view,
            Boolean(objectView.item?.values && objectView.item?.values?.length) && (React.createElement(rebass_1.Box, { mb: 2, className: `${baseClassName}__Item__Values` },
                React.createElement(ValueSelector_1.ValueOptionsTags, { style: { marginRight: 0 }, readOnly: true, options: objectView.item.values, value: objectView.item.values, allowWrap: true, toIdentifier: (c) => c, toLabel: (c) => React.createElement(React.Fragment, null, c) })))),
        suspendAction && unSuspendAction && (React.createElement(SimpleButton_1.default, { onMouseDown: handleSuspendUnSuspend, tone: isSuspended ? 'neutral' : 'success', variant: "text", icon: isSuspended ? 'resume' : 'pause' })),
        React.createElement(rebass_1.Flex, { ml: 1, flex: '0 0 auto' }, deleteAction && (React.createElement(SimpleButton_1.default, { iconSize: ICON_SIZE, icon: "delete", variant: "text", onMouseDown: handleDelete, accessLevel: moduleAccessLevel })))));
};
exports.AdaptableObjectCompactListItem = AdaptableObjectCompactListItem;
const AdaptableObjectCompactList = (props) => {
    const adaptable = (0, AdaptableContext_1.useAdaptable)();
    const viewProperties = props.module.getViewProperties?.();
    const deleteAllAction = viewProperties?.getDeleteAllAction?.();
    const suspendAllAction = viewProperties?.getSuspendAllAction?.();
    const unSuspendAllAction = viewProperties?.getUnSuspendAllAction?.();
    const dispatch = (0, react_redux_1.useDispatch)();
    const accessLevel = adaptable.api.entitlementApi.getEntitlementAccessLevelForModule(props.module.moduleInfo.ModuleName);
    const isAtLeastOneAbObjectActive = props.abObjects.some((abObject) => {
        return !abObject.IsSuspended;
    });
    const handleSuspendUnsuspendAll = React.useCallback((event) => {
        event.stopPropagation();
        event.preventDefault();
        if (isAtLeastOneAbObjectActive) {
            dispatch(suspendAllAction);
        }
        else {
            dispatch(unSuspendAllAction);
        }
    }, [isAtLeastOneAbObjectActive]);
    return (React.createElement(rebass_1.Flex, { flexDirection: "column", className: baseClassName },
        React.createElement(rebass_1.Flex, { className: `${baseClassName}__Header`, fontSize: 3 },
            React.createElement(rebass_1.Flex, { className: `${baseClassName}__Title`, alignItems: 'center' }, props.module.moduleInfo.FriendlyName),
            React.createElement(rebass_1.Box, { flex: 1 }),
            suspendAllAction && unSuspendAllAction && (React.createElement(SimpleButton_1.default, { mr: 2, onMouseDown: handleSuspendUnsuspendAll, tone: isAtLeastOneAbObjectActive ? 'neutral' : 'success', variant: "raised", icon: isAtLeastOneAbObjectActive ? 'pause' : 'resume', accessLevel: accessLevel }, isAtLeastOneAbObjectActive ? 'Suspend All' : 'Unsuspend All')),
            deleteAllAction && (React.createElement(SimpleButton_1.default, { onMouseDown: () => dispatch(deleteAllAction), variant: "raised", tone: "neutral", accessLevel: accessLevel }, "Clear All"))),
        React.createElement(rebass_1.Box, { className: `${baseClassName}__Body` }, props.abObjects.map((abObject) => {
            return (React.createElement(exports.AdaptableObjectCompactListItem, { key: abObject.Uuid, abObject: abObject, module: props.module }));
        }))));
};
exports.AdaptableObjectCompactList = AdaptableObjectCompactList;
