import { jsx as _jsx } from "react/jsx-runtime";
// (C) 2025 GoodData Corporation
import { useCallback, useMemo } from "react";
import { cloneDeep } from "lodash-es";
import { useIntl } from "react-intl";
import { UiCheckbox } from "@gooddata/sdk-ui-kit";
import { messages } from "../../../locales.js";
/**
 * Single text wrapping checkbox control.
 * Supports tri-state checkbox that shows indeterminate when global is ON but column overrides exist.
 *
 * @internal
 */
function TextWrappingCheckbox({ properties, pushData, disabled = false, wrapType, messageKey, testId, }) {
    const intl = useIntl();
    const textWrapping = properties?.controls?.["textWrapping"] ?? {};
    const columnOverrides = useMemo(() => textWrapping?.columnOverrides ?? [], [textWrapping?.columnOverrides]);
    // Determine checkbox state
    const checked = !!textWrapping?.[wrapType];
    // Indeterminate state: global is ON but some columns have it turned OFF
    // This represents "mostly on, but with exceptions"
    const hasOverrides = useMemo(() => checked &&
        columnOverrides.some((item) => {
            const overrideValue = item[wrapType];
            // Only count as override if explicitly set to false when global is true
            return overrideValue !== undefined && !overrideValue;
        }), [columnOverrides, wrapType, checked]);
    const indeterminate = hasOverrides;
    // Update global wrapping setting and clear corresponding overrides
    const updateWrapping = useCallback((event) => {
        const clonedProperties = cloneDeep(properties) || {};
        clonedProperties.controls = clonedProperties.controls || {};
        const currentTextWrapping = clonedProperties.controls["textWrapping"] || {};
        const currentOverrides = currentTextWrapping.columnOverrides ?? [];
        // Clear the corresponding override property from all overrides
        // If changing wrapText, clear wrapText from overrides but keep wrapHeaderText
        // If changing wrapHeaderText, clear wrapHeaderText from overrides but keep wrapText
        const cleanedOverrides = currentOverrides
            .map((item) => {
            const { [wrapType]: _, ...rest } = item;
            return rest;
        })
            .filter((item) => {
            // Remove empty override objects (those with no properties)
            return Object.keys(item).some((key) => {
                const value = item[key];
                return value !== undefined;
            });
        });
        clonedProperties.controls["textWrapping"] = {
            ...currentTextWrapping,
            [wrapType]: event.target.checked,
            columnOverrides: cleanedOverrides,
        };
        pushData({ properties: clonedProperties });
    }, [properties, pushData, wrapType]);
    const label = intl.formatMessage(messages[messageKey]);
    return (_jsx("div", { className: `s-pivot-text-wrapping-${testId} gd-pivot-text-wrapping-checkbox`, children: _jsx(UiCheckbox, { checked: checked, indeterminate: indeterminate, disabled: disabled, onChange: updateWrapping, label: label }) }));
}
/**
 * Header text wrapping control for PivotTableNext Column Headers section.
 *
 * @internal
 */
export function ColumnHeaderTextWrappingControl({ properties, pushData, disabled = false, }) {
    return (_jsx(TextWrappingCheckbox, { properties: properties, pushData: pushData, disabled: disabled, wrapType: "wrapHeaderText", messageKey: "columnHeaderWrapText", testId: "header" }));
}
/**
 * Cell text wrapping control for PivotTableNext Cells section.
 *
 * @internal
 */
export function CellTextWrappingControl({ properties, pushData, disabled = false, }) {
    return (_jsx(TextWrappingCheckbox, { properties: properties, pushData: pushData, disabled: disabled, wrapType: "wrapText", messageKey: "cellsWrapText", testId: "cells" }));
}
//# sourceMappingURL=PivotTableTextWrappingControl.js.map