import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
// (C) 2024-2025 GoodData Corporation
import { useCallback, useMemo } from "react";
import { FormattedMessage, useIntl } from "react-intl";
import { DialogListHeader, Message } from "@gooddata/sdk-ui-kit";
import { AdditionalAccessPermissionItem, WorkspaceAccessPermissionItem } from "./GranularPermissionsItems.js";
import { getGranularPermissions, getImplicitGranularPermissions, getWorkspacePermission, isExportPermissionIndefinite, isPermissionDisabled, removeRedundantPermissions, sanitizeExportPermissions, workspacePermissions, } from "./granularPermissionUtils.js";
import { getWorkspaceAccessPermissionDescription, workspaceGranularPermissionMessages } from "./locales.js";
import { QuestionMarkIcon } from "./QuestionMarkIcon.js";
const granularPermissions = [
    { id: "CREATE_AUTOMATION", enabled: true },
    { id: "USE_AI_ASSISTANT", enabled: true },
    { id: "EXPORT", enabled: true },
    { id: "EXPORT_PDF", enabled: true, group: true },
    { id: "EXPORT_TABULAR", enabled: true, group: true },
    { id: "CREATE_FILTER_VIEW", enabled: true },
];
export function GranularPermissions({ workspace, areFilterViewsEnabled, onChange, showRedundancyWarningMessage, }) {
    const intl = useIntl();
    const { permissions: selectedPermissions = [], isHierarchical = false } = workspace ?? {};
    const selectedWorkspacePermission = getWorkspacePermission(selectedPermissions);
    const selectedGranularPermissions = getGranularPermissions(selectedPermissions);
    const granularItems = useMemo(() => granularPermissions.filter((item) => areFilterViewsEnabled || item.id !== "CREATE_FILTER_VIEW"), [areFilterViewsEnabled]);
    const handleChange = useCallback((permissions, isHierarchical) => {
        const sanitizedPermissions = removeRedundantPermissions(permissions);
        onChange({ ...workspace, permissions: sanitizedPermissions, isHierarchical });
    }, [onChange, workspace]);
    const handleHierarchicalChange = useCallback((isHierarchical) => {
        handleChange([selectedWorkspacePermission, ...selectedGranularPermissions], isHierarchical);
    }, [handleChange, selectedGranularPermissions, selectedWorkspacePermission]);
    const handleWorkspacePermissionChange = useCallback((workspacePermission) => {
        const updatedGranularPermissions = getImplicitGranularPermissions(workspacePermission);
        handleChange([workspacePermission, ...updatedGranularPermissions], isHierarchical);
    }, [handleChange, isHierarchical]);
    const handleGranularPermissionChange = useCallback((granularPermission) => {
        const isPermissionSelected = selectedGranularPermissions.includes(granularPermission);
        const updatedGranularPermissions = isPermissionSelected
            ? selectedGranularPermissions.filter((p) => p !== granularPermission)
            : [...selectedGranularPermissions, granularPermission];
        const sanitizedGranularPermissions = sanitizeExportPermissions(granularPermission, updatedGranularPermissions, !isPermissionSelected);
        handleChange([selectedWorkspacePermission, ...sanitizedGranularPermissions], isHierarchical);
    }, [handleChange, isHierarchical, selectedGranularPermissions, selectedWorkspacePermission]);
    if (!workspace) {
        return (_jsx("div", { className: "gd-granular-permissions-empty", children: _jsx(FormattedMessage, { id: "userManagement.workspace.granularPermission.noWorkspace" }) }));
    }
    return (_jsxs("div", { className: "gd-granular-permissions", children: [_jsxs("div", { className: "gd-granular-permissions__hierarchy", children: [_jsxs("label", { className: "input-checkbox-toggle", children: [_jsx("input", { type: "checkbox", checked: isHierarchical, onChange: (e) => handleHierarchicalChange(e.currentTarget.checked) }), _jsx("span", { className: "input-label-text", children: _jsx(FormattedMessage, { id: "userManagement.workspace.granularPermission.hierarchy" }) })] }), _jsx("div", { className: "gd-granular-permissions__hierarchy-icon", children: _jsx(QuestionMarkIcon, { bubbleTextId: workspaceGranularPermissionMessages.hierarchyTooltip.id, width: 14, height: 14 }) })] }), _jsx(DialogListHeader, { title: intl.formatMessage({
                    id: "userManagement.workspace.granularPermission.workspaceAccess.title",
                }) }), _jsx("div", { className: "gd-granular-permissions__workspace-access", children: workspacePermissions.map((workspacePermission) => {
                    return (_jsx(WorkspaceAccessPermissionItem, { item: workspacePermission, checked: workspacePermission === selectedWorkspacePermission, onChange: () => handleWorkspacePermissionChange(workspacePermission) }, workspacePermission));
                }) }), _jsx("div", { className: "gd-granular-permissions__workspace-access-description", children: _jsx(FormattedMessage, { id: getWorkspaceAccessPermissionDescription(selectedWorkspacePermission).id }) }), _jsx(DialogListHeader, { title: intl.formatMessage({
                    id: "userManagement.workspace.granularPermission.additionalAccess.title",
                }) }), _jsx("div", { className: "gd-granular-permissions__additional-access-wrapper", children: granularItems.map((granularPermission) => {
                    return (_jsx(AdditionalAccessPermissionItem, { item: granularPermission, checked: selectedGranularPermissions.includes(granularPermission.id), indefinite: isExportPermissionIndefinite(granularPermission.id, selectedGranularPermissions), disabled: isPermissionDisabled(granularPermission.id, selectedWorkspacePermission, selectedGranularPermissions), onChange: () => handleGranularPermissionChange(granularPermission.id) }, granularPermission.id));
                }) }), showRedundancyWarningMessage ? (_jsx(Message, { type: "warning", className: "gd-granular-permissions__warning", children: _jsx(FormattedMessage, { id: "userManagement.workspace.granularPermission.warning", values: {
                        b: (chunks) => _jsx("strong", { children: chunks }),
                    } }) })) : null] }));
}
//# sourceMappingURL=GranularPermissions.js.map