"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 __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.DataGridPreferencesColumnSettings = DataGridPreferencesColumnSettings;
const react_1 = __importStar(require("react"));
const DragAndDropRoot_1 = __importDefault(require("../../data/drag-and-drop/root/DragAndDropRoot"));
const checkbox_1 = require("../../form/checkbox");
const fieldset_1 = require("../../form/fieldset");
function DataGridPreferencesColumnSettings({ columns, onColumnsChange, }) {
    const visibleCount = columns.filter((c) => !!c.visible).length;
    const isAllVisible = visibleCount === columns.length;
    const dndItems = (0, react_1.useMemo)(() => columns.map(({ id, label }) => ({ id, label })), [columns]);
    const colMap = (0, react_1.useMemo)(() => new Map(columns.map((c) => [c.id, c])), [columns]);
    const setDndItems = (0, react_1.useCallback)((items) => {
        const newItems = items;
        onColumnsChange(newItems.flatMap((item) => {
            const col = colMap.get(item.id);
            return col ? [col] : [];
        }));
    }, [colMap, onColumnsChange]);
    const toggleAll = (0, react_1.useCallback)(() => {
        const newVisible = !isAllVisible;
        onColumnsChange(columns.map((c) => (Object.assign(Object.assign({}, c), { visible: c.visible === "always" ? "always" : newVisible }))));
    }, [columns, isAllVisible, onColumnsChange]);
    const toggleColumn = (0, react_1.useCallback)((id) => {
        const col = colMap.get(id);
        if ((col === null || col === void 0 ? void 0 : col.visible) === "always")
            return;
        onColumnsChange(columns.map((c) => (c.id === id ? Object.assign(Object.assign({}, c), { visible: !c.visible }) : c)));
    }, [colMap, columns, onColumnsChange]);
    return (react_1.default.createElement(fieldset_1.Fieldset, { legend: "Kolonner", size: "small" },
        react_1.default.createElement(checkbox_1.Checkbox, { size: "small", checked: isAllVisible, indeterminate: visibleCount > 0 && !isAllVisible, onChange: toggleAll }, `Velg alle (${visibleCount}/${columns.length})`),
        react_1.default.createElement(DragAndDropRoot_1.default, { className: "aksel-data-grid__preferences-dnd", items: dndItems, setItems: setDndItems, renderItem: (item) => {
                var _a, _b;
                const visibleState = (_b = (_a = colMap.get(item.id)) === null || _a === void 0 ? void 0 : _a.visible) !== null && _b !== void 0 ? _b : true;
                const alwaysVisible = visibleState === "always";
                return (react_1.default.createElement(checkbox_1.Checkbox, { size: "small", checked: visibleState !== false, onChange: () => toggleColumn(item.id), disabled: alwaysVisible }, item.label));
            } })));
}
//# sourceMappingURL=DataGridPreferencesColumnSettings.js.map