import React from 'react';
import { SortAscendingSmall, SortDescendingSmall, ContentFilterSmall, HiddenSmall, DeleteSmall, EditSmall, FlagSmall, DuplicateSmall, } from '@wix/wix-ui-icons-common';
const getReadOnlySchemaReason = (perms) => perms?.schemaEditable === false ? 'schemaNotEditable' : null;
const checkEditPerms = (col, perms) => {
    const blocked = getReadOnlySchemaReason(perms);
    if (blocked) {
        return blocked;
    }
    if (perms?.editDisabled) {
        return 'editDisabled';
    }
    if (col.isSystem) {
        return 'systemField';
    }
    return null;
};
const checkAddPerms = (col, perms) => {
    const blocked = getReadOnlySchemaReason(perms);
    if (blocked) {
        return blocked;
    }
    if (perms?.addDisabled) {
        return 'addDisabled';
    }
    if (col.isSystem) {
        return 'systemField';
    }
    return null;
};
const checkDeletePerms = (col, perms) => {
    const blocked = getReadOnlySchemaReason(perms);
    if (blocked) {
        return blocked;
    }
    if (perms?.deleteDisabled) {
        return 'deleteDisabled';
    }
    if (col.isSystem) {
        return 'systemField';
    }
    if (col.isPrimary) {
        return 'alreadyPrimary';
    }
    return null;
};
export const FIELD_ACTIONS = {
    editField: {
        label: 'Edit Field',
        icon: React.createElement(EditSmall, null),
        getDisableReason: checkEditPerms,
    },
    makePrimary: {
        label: 'Use as Primary Field',
        icon: React.createElement(FlagSmall, null),
        getDisableReason: (col, perms) => {
            const blocked = getReadOnlySchemaReason(perms);
            if (blocked) {
                return blocked;
            }
            if (perms?.editDisabled) {
                return 'editDisabled';
            }
            if (col.isPrimary) {
                return 'alreadyPrimary';
            }
            if (col.isSystem) {
                return 'systemField';
            }
            if (col.cellType !== 'text') {
                return 'nonTextField';
            }
            return null;
        },
    },
    hideColumn: {
        label: 'Hide Column',
        icon: React.createElement(HiddenSmall, null),
    },
    duplicateField: {
        label: 'Duplicate Field',
        icon: React.createElement(DuplicateSmall, null),
        getDisableReason: checkAddPerms,
    },
    duplicateFieldWithContent: {
        label: 'Duplicate Field with Content',
        icon: React.createElement(DuplicateSmall, null),
        getDisableReason: checkAddPerms,
    },
    sortAscending: {
        label: 'Sort Ascending',
        icon: React.createElement(SortAscendingSmall, null),
    },
    sortDescending: {
        label: 'Sort Descending',
        icon: React.createElement(SortDescendingSmall, null),
    },
    filter: {
        label: 'Filter',
        icon: React.createElement(ContentFilterSmall, null),
    },
    delete: {
        label: 'Delete',
        icon: React.createElement(DeleteSmall, null),
        skin: 'destructive',
        getDisableReason: checkDeletePerms,
    },
};
export const FIELD_ACTION_DISABLE_REASON_TOOLTIPS = {
    schemaNotEditable: 'This schema is read-only.',
    addDisabled: 'Adding fields is disabled.',
    editDisabled: 'Editing fields is disabled.',
    deleteDisabled: 'Deleting fields is disabled.',
    alreadyPrimary: 'This is the primary field.',
    systemField: "System fields can't be modified.",
    nonTextField: 'Only text fields can be made primary.',
};
export function getFieldActionDisableReason(action, column, permissions) {
    return FIELD_ACTIONS[action].getDisableReason?.(column, permissions) ?? null;
}
export function getFieldActionDisableState(action, column, permissions) {
    const reason = getFieldActionDisableReason(action, column, permissions);
    return {
        disabled: reason !== null,
        disabledTooltip: reason
            ? FIELD_ACTION_DISABLE_REASON_TOOLTIPS[reason]
            : undefined,
    };
}
export function buildFieldActionGroups(args) {
    const { column, permissions, handlers, loading, groups } = args;
    return groups
        .map((group) => group.flatMap((key) => {
        const handler = handlers[key];
        if (!handler) {
            return [];
        }
        const def = FIELD_ACTIONS[key];
        const disable = getFieldActionDisableState(key, column, permissions);
        return [
            {
                label: def.label,
                icon: def.icon,
                skin: def.skin,
                onClick: () => handler(column.id),
                loading: loading?.[key],
                ...disable,
            },
        ];
    }))
        .filter((g) => g.length > 0);
}
//# sourceMappingURL=FieldActions.js.map