"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
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;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.DataGridPreferencesRoot = void 0;
const react_1 = __importStar(require("react"));
const aksel_icons_1 = require("@navikt/aksel-icons");
const button_1 = require("../../button");
const DataGridRoot_context_1 = require("../../data-grid/root/DataGridRoot.context");
const dialog_1 = require("../../dialog");
const helpers_1 = require("../../utils/helpers");
const DataGridPreferencesColumnLayoutSettings_1 = require("../column-layout-settings/DataGridPreferencesColumnLayoutSettings");
const DataGridPreferencesColumnSettings_1 = require("../column-settings/DataGridPreferencesColumnSettings");
const DataGridPreferencesRowDensitySettings_1 = require("../row-density-settings/DataGridPreferencesRowDensitySettings");
const DataGridPreferencesRowPropertiesSettings_1 = require("../row-properties-settings/DataGridPreferencesRowPropertiesSettings");
const DataGridPreferencesTextSizeSettings_1 = require("../text-size-settings/DataGridPreferencesTextSizeSettings");
/**
 * 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 = (0, react_1.forwardRef)((_a, forwardedRef) => {
    var { className, fields, rootElement, open, defaultOpen, onOpenChange, onOpenChangeComplete } = _a, rest = __rest(_a, ["className", "fields", "rootElement", "open", "defaultOpen", "onOpenChange", "onOpenChangeComplete"]);
    const context = (0, DataGridRoot_context_1.useDataGridContext)(true, "[Aksel] DataGrid.Preferences must be used within DataGrid");
    const { tableSettings, updateTableSettings, columnDefinitions } = context;
    const columnDefinitionMap = (0, react_1.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 = (0, react_1.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 = (0, react_1.useCallback)((columns) => {
        updateTableSettings === null || updateTableSettings === void 0 ? void 0 : updateTableSettings({
            columnDisplay: columns.map(({ id, visible }) => ({ id, visible })),
        });
    }, [updateTableSettings]);
    return (react_1.default.createElement(dialog_1.Dialog, { open: open, onOpenChange: onOpenChange, defaultOpen: defaultOpen, onOpenChangeComplete: onOpenChangeComplete, size: "small" },
        react_1.default.createElement(dialog_1.DialogTrigger, null,
            react_1.default.createElement(button_1.Button, Object.assign({ ref: forwardedRef, "aria-label": "\u00C5pne innstillinger for tabell" }, rest, { variant: "tertiary", size: "small", "data-color": "neutral", icon: react_1.default.createElement(aksel_icons_1.CogIcon, null), className: (0, helpers_1.cl)("aksel-data-grid__preferences-button", className) }))),
        react_1.default.createElement(dialog_1.DialogPopup, { width: "large", position: "right", rootElement: rootElement },
            react_1.default.createElement(dialog_1.DialogHeader, { withClosebutton: true },
                react_1.default.createElement(dialog_1.DialogTitle, null, "Innstillinger")),
            react_1.default.createElement(dialog_1.DialogBody, { className: "aksel-data-grid__preferences-body" },
                react_1.default.createElement("div", { className: "aksel-data-grid__preferences-content" },
                    react_1.default.createElement("div", { className: "aksel-data-grid__preferences-block" },
                        isFieldVisible("rowDensity", fields) && (react_1.default.createElement(DataGridPreferencesRowDensitySettings_1.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_1.default.createElement(DataGridPreferencesTextSizeSettings_1.DataGridPreferencesTextSizeSettings, { value: tableSettings.textSize, onChange: (value) => updateTableSettings === null || updateTableSettings === void 0 ? void 0 : updateTableSettings({ textSize: value }) })),
                        react_1.default.createElement(DataGridPreferencesRowPropertiesSettings_1.DataGridPreferencesRowPropertiesSettings, { fields: {
                                truncateContent: isFieldVisible("truncateContent", fields),
                                zebraStripes: isFieldVisible("zebraStripes", fields),
                            }, value: {
                                truncateContent: tableSettings.truncateContent,
                                zebraStripes: tableSettings.zebraStripes,
                            }, onChange: updateTableSettings }),
                        react_1.default.createElement(DataGridPreferencesColumnLayoutSettings_1.DataGridPreferencesColumnLayoutSettings, { fields: {
                                columnDividers: isFieldVisible("columnDividers", fields),
                                stickyColumns: isFieldVisible("stickyColumns", fields),
                            }, value: {
                                columnDividers: tableSettings.columnDividers,
                                stickyColumns: tableSettings.stickyColumns,
                            }, onChange: updateTableSettings })),
                    react_1.default.createElement("div", { className: "aksel-data-grid__preferences-block" }, isFieldVisible("columnDisplay", fields) && (react_1.default.createElement(DataGridPreferencesColumnSettings_1.DataGridPreferencesColumnSettings, { columns: columnEntries, onColumnsChange: handleColumnsChange }))))))));
});
exports.DataGridPreferencesRoot = DataGridPreferencesRoot;
function isFieldVisible(key, fields) {
    return (fields === null || fields === void 0 ? void 0 : fields[key]) !== false;
}
exports.default = DataGridPreferencesRoot;
//# sourceMappingURL=DataGridPreferencesRoot.js.map