"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.FormGroupGrid = void 0;
const react_1 = __importDefault(require("react"));
const varsUtilities_1 = require("../../../varsUtilities");
const __1 = require("../..");
const Item_1 = require("./Item");
const utils_1 = require("../utils");
const inlines_1 = require("inlines");
const Group = ({ children, style, }) => {
    return (react_1.default.createElement(__1.RowSpaced, { style: {
            ...style,
            borderTop: (0, varsUtilities_1.border)(1),
            marginLeft: -8,
            marginRight: -8,
            marginTop: 16,
            paddingTop: 8,
            flexWrap: 'wrap',
        } }, children));
};
const FormGroupGrid = ({ onChange, parsedData, fieldWidth, onChangeField, labelWidth, hasChanges, valuesChanged, values, alwaysAccept, setChanges, autoFocus, style, confirmationLabel, confirmationVariant, }) => {
    const checkBoxes = [];
    const rest = [];
    let hasAutoFocus = false;
    for (const d of parsedData) {
        if (d.type === 'checkbox') {
            checkBoxes.push(react_1.default.createElement(Item_1.FormItem, { fieldWidth: fieldWidth, width: labelWidth, key: d.field, item: d, onChange: onChangeField, value: d.value ??
                    (hasChanges
                        ? (0, utils_1.getValue)(d.field, valuesChanged.current) ??
                            d.value ??
                            (0, utils_1.getValue)(d.field, values)
                        : (0, utils_1.getValue)(d.field, values)), onChangeObj: onChange, objValues: values, hasChanges: hasChanges, valuesChanged: valuesChanged, setChanges: setChanges, alwaysAccept: alwaysAccept }));
        }
        else {
            rest.push(react_1.default.createElement(Item_1.FormItem, { autoFocus: !hasAutoFocus && autoFocus, fieldWidth: fieldWidth, width: labelWidth, key: d.field, item: d, onChange: onChangeField, value: hasChanges
                    ? (0, utils_1.getValue)(d.field, valuesChanged.current) ??
                        d.value ??
                        (0, utils_1.getValue)(d.field, values)
                    : d.value ?? (0, utils_1.getValue)(d.field, values), onChangeObj: onChange, objValues: values, hasChanges: hasChanges, valuesChanged: valuesChanged, setChanges: setChanges, alwaysAccept: alwaysAccept }));
            hasAutoFocus = true;
        }
    }
    return (react_1.default.createElement(Group, { style: style },
        react_1.default.createElement(inlines_1.styled.div, { style: {
                display: 'grid',
                gridTemplateColumns: `repeat(auto-fill, minmax(${fieldWidth * 2}px, 2fr))`,
                gridGap: 12,
                width: '100%',
            } }, rest),
        checkBoxes.length && rest.length ? (react_1.default.createElement(__1.Row, { style: {
                width: '100%',
                borderTop: (0, varsUtilities_1.border)(1),
                marginTop: 16,
                padding: 8,
                paddingTop: 16,
                flexWrap: 'wrap',
            } }, checkBoxes)) : (checkBoxes),
        react_1.default.createElement(inlines_1.styled.div, { style: { width: fieldWidth * 2 + 'px' } }),
        alwaysAccept || !hasChanges ? null : (react_1.default.createElement(__1.RowEnd, { style: {
                borderTop: (0, varsUtilities_1.border)(1),
                width: '100%',
                marginTop: 16,
                paddingTop: 16,
                marginRight: 8,
            } },
            react_1.default.createElement(__1.Text, { light: true }, "Apply changes"),
            react_1.default.createElement(__1.Confirmation, { variant: confirmationVariant === 'none' || confirmationVariant === 'modal'
                    ? 'buttons'
                    : confirmationVariant, label: confirmationLabel, onCancel: () => {
                    valuesChanged.current = {};
                    setChanges(false);
                }, onConfirm: async () => {
                    await onChange(valuesChanged.current);
                    valuesChanged.current = {};
                    setChanges(false);
                } })))));
};
exports.FormGroupGrid = FormGroupGrid;
//# sourceMappingURL=index.js.map