import React from 'react';
import { useDispatch } from 'react-redux';
import SimpleButton from '../components/SimpleButton';
export const getObjectTagsViewItems = (object, api) => {
    if (!api.internalApi.shouldDisplayTagSections() || !object?.Tags?.length) {
        return;
    }
    const currentLayoutName = api.layoutApi.getCurrentLayoutName();
    const hasCurrentLayoutAsTag = (object?.Tags ?? [])?.includes(currentLayoutName);
    return {
        name: 'Tags',
        values: object?.Tags?.map((tag) => api.internalApi.getLabelForTag(tag)),
        viewAfter: (props) => {
            const dispatch = useDispatch();
            const viewOptions = props.module.getViewProperties();
            const handleToggleTag = React.useCallback(() => {
                let tags = props.data.Tags ?? [];
                if (hasCurrentLayoutAsTag) {
                    // remove layout
                    tags = tags.filter((tag) => tag !== currentLayoutName);
                }
                else {
                    tags.push(currentLayoutName);
                }
                const newObject = {
                    ...props.data,
                    Tags: tags,
                };
                const editAction = viewOptions.getEditAction(newObject);
                dispatch(editAction);
            }, [hasCurrentLayoutAsTag, props.data]);
            return (React.createElement(SimpleButton, { onClick: handleToggleTag, mt: 1, icon: hasCurrentLayoutAsTag ? 'minus' : 'plus' }, hasCurrentLayoutAsTag ? 'Remove from Layout' : 'Add to Layout'));
        },
    };
};
