"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getObjectTagsViewItems = void 0;
const tslib_1 = require("tslib");
const react_1 = tslib_1.__importDefault(require("react"));
const SimpleButton_1 = tslib_1.__importDefault(require("../../components/SimpleButton"));
const react_redux_1 = require("react-redux");
const getObjectTagsViewItems = (object, api) => {
    var _a, _b, _c, _d;
    if (!api.internalApi.shouldDisplayTagSections() || !((_a = object === null || object === void 0 ? void 0 : object.Tags) === null || _a === void 0 ? void 0 : _a.length)) {
        return;
    }
    const currentLayoutName = api.layoutApi.getCurrentLayoutName();
    const hasCurrentLayoutAsTag = (_c = ((_b = object === null || object === void 0 ? void 0 : object.Tags) !== null && _b !== void 0 ? _b : [])) === null || _c === void 0 ? void 0 : _c.includes(currentLayoutName);
    return {
        name: 'Tags',
        values: (_d = object === null || object === void 0 ? void 0 : object.Tags) === null || _d === void 0 ? void 0 : _d.map((tag) => api.internalApi.getLabelForTag(tag)),
        viewAfter: (props) => {
            const dispatch = (0, react_redux_1.useDispatch)();
            const viewOptions = props.module.getViewProperties();
            const handleToggleTag = react_1.default.useCallback(() => {
                var _a;
                let tags = (_a = props.data.Tags) !== null && _a !== void 0 ? _a : [];
                if (hasCurrentLayoutAsTag) {
                    // remove layout
                    tags = tags.filter((tag) => tag !== currentLayoutName);
                }
                else {
                    tags.push(currentLayoutName);
                }
                const newObject = Object.assign(Object.assign({}, props.data), { Tags: tags });
                const editAction = viewOptions.getEditAction(newObject);
                dispatch(editAction);
            }, [hasCurrentLayoutAsTag, props.data]);
            return (react_1.default.createElement(SimpleButton_1.default, { onClick: handleToggleTag, mt: 1, icon: hasCurrentLayoutAsTag ? 'minus' : 'plus' }, hasCurrentLayoutAsTag ? 'Remove from Layout' : 'Add to Layout'));
        },
    };
};
exports.getObjectTagsViewItems = getObjectTagsViewItems;
