var __rest = (this && this.__rest) || function (s, e) {
    var t = {};
    for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
        t[p] = s[p];
    if (s != null && typeof Object.getOwnPropertySymbols === "function")
        for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
            if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
                t[p[i]] = s[p[i]];
        }
    return t;
};
import React, { forwardRef, useCallback, useMemo } from "react";
import { CogIcon } from "@navikt/aksel-icons";
import { Button } from "../../button/index.js";
import { useDataGridContext } from "../../data-grid/root/DataGridRoot.context.js";
import { Dialog, DialogBody, DialogHeader, DialogPopup, DialogTitle, DialogTrigger, } from "../../dialog/index.js";
import { cl } from "../../utils/helpers/index.js";
import { DataGridPreferencesColumnLayoutSettings } from "../column-layout-settings/DataGridPreferencesColumnLayoutSettings.js";
import { DataGridPreferencesColumnSettings, } from "../column-settings/DataGridPreferencesColumnSettings.js";
import { DataGridPreferencesRowDensitySettings } from "../row-density-settings/DataGridPreferencesRowDensitySettings.js";
import { DataGridPreferencesRowPropertiesSettings } from "../row-properties-settings/DataGridPreferencesRowPropertiesSettings.js";
import { DataGridPreferencesTextSizeSettings } from "../text-size-settings/DataGridPreferencesTextSizeSettings.js";
/**
 * Component for displaying preferences/settings for DataGrid.
 *
 * **WARNING: This component is in active development and may receive breaking changes outside major releases!**
 *
 * @see [📝 Documentation](https://aksel.nav.no/komponenter/core/datagrid)
 * @see 🏷️ {@link DataGridPreferences}
 *
 * @example
 * ```jsx
 * <DataGrid columns={columns} data={data} onSettingsChange={handleSettingsChange}>
 *   <DataGrid.Preferences />
 *   <DataGrid.Table />
 * </DataGrid>
 * ```
 */
const DataGridPreferencesRoot = forwardRef((_a, forwardedRef) => {
    var { className, fields, rootElement, open, defaultOpen, onOpenChange, onOpenChangeComplete } = _a, rest = __rest(_a, ["className", "fields", "rootElement", "open", "defaultOpen", "onOpenChange", "onOpenChangeComplete"]);
    const context = useDataGridContext(true, "[Aksel] DataGrid.Preferences must be used within DataGrid");
    const { tableSettings, updateTableSettings, columnDefinitions } = context;
    const columnDefinitionMap = useMemo(() => new Map(columnDefinitions.map((col) => [col.id, col.header])), [columnDefinitions]);
    /**
     * Combines the saved column order/visibility (`tableSettings.columnDisplay`)
     * with column labels from `columnDefinitions`. Falls back to all columns
     * visible in definition order when `columnDisplay` is unset.
     */
    const columnEntries = useMemo(() => {
        var _a;
        const display = (_a = tableSettings === null || tableSettings === void 0 ? void 0 : tableSettings.columnDisplay) !== null && _a !== void 0 ? _a : columnDefinitions.map((col) => ({ id: col.id, visible: true }));
        return display.flatMap(({ id, visible }) => {
            const label = columnDefinitionMap.get(id);
            return label ? [{ id, label, visible }] : [];
        });
    }, [columnDefinitionMap, tableSettings === null || tableSettings === void 0 ? void 0 : tableSettings.columnDisplay, columnDefinitions]);
    const handleColumnsChange = useCallback((columns) => {
        updateTableSettings === null || updateTableSettings === void 0 ? void 0 : updateTableSettings({
            columnDisplay: columns.map(({ id, visible }) => ({ id, visible })),
        });
    }, [updateTableSettings]);
    return (React.createElement(Dialog, { open: open, onOpenChange: onOpenChange, defaultOpen: defaultOpen, onOpenChangeComplete: onOpenChangeComplete, size: "small" },
        React.createElement(DialogTrigger, null,
            React.createElement(Button, Object.assign({ ref: forwardedRef, "aria-label": "\u00C5pne innstillinger for tabell" }, rest, { variant: "tertiary", size: "small", "data-color": "neutral", icon: React.createElement(CogIcon, null), className: cl("aksel-data-grid__preferences-button", className) }))),
        React.createElement(DialogPopup, { width: "large", position: "right", rootElement: rootElement },
            React.createElement(DialogHeader, { withClosebutton: true },
                React.createElement(DialogTitle, null, "Innstillinger")),
            React.createElement(DialogBody, { className: "aksel-data-grid__preferences-body" },
                React.createElement("div", { className: "aksel-data-grid__preferences-content" },
                    React.createElement("div", { className: "aksel-data-grid__preferences-block" },
                        isFieldVisible("rowDensity", fields) && (React.createElement(DataGridPreferencesRowDensitySettings, { value: tableSettings === null || tableSettings === void 0 ? void 0 : tableSettings.rowDensity, onChange: (value) => updateTableSettings === null || updateTableSettings === void 0 ? void 0 : updateTableSettings({ rowDensity: value }) })),
                        isFieldVisible("textSize", fields) && (React.createElement(DataGridPreferencesTextSizeSettings, { value: tableSettings.textSize, onChange: (value) => updateTableSettings === null || updateTableSettings === void 0 ? void 0 : updateTableSettings({ textSize: value }) })),
                        React.createElement(DataGridPreferencesRowPropertiesSettings, { fields: {
                                truncateContent: isFieldVisible("truncateContent", fields),
                                zebraStripes: isFieldVisible("zebraStripes", fields),
                            }, value: {
                                truncateContent: tableSettings.truncateContent,
                                zebraStripes: tableSettings.zebraStripes,
                            }, onChange: updateTableSettings }),
                        React.createElement(DataGridPreferencesColumnLayoutSettings, { fields: {
                                columnDividers: isFieldVisible("columnDividers", fields),
                                stickyColumns: isFieldVisible("stickyColumns", fields),
                            }, value: {
                                columnDividers: tableSettings.columnDividers,
                                stickyColumns: tableSettings.stickyColumns,
                            }, onChange: updateTableSettings })),
                    React.createElement("div", { className: "aksel-data-grid__preferences-block" }, isFieldVisible("columnDisplay", fields) && (React.createElement(DataGridPreferencesColumnSettings, { columns: columnEntries, onColumnsChange: handleColumnsChange }))))))));
});
function isFieldVisible(key, fields) {
    return (fields === null || fields === void 0 ? void 0 : fields[key]) !== false;
}
// eslint-disable-next-line import/export
export { DataGridPreferencesRoot };
export default DataGridPreferencesRoot;
//# sourceMappingURL=DataGridPreferencesRoot.js.map