"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 (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ObjectItem = void 0;
const react_1 = __importStar(require("react"));
const components_1 = require("../../components");
const Item_1 = require("./Column/Item");
const inlines_1 = require("inlines");
const utils_1 = require("./utils");
const kabouter_1 = require("kabouter");
const ObjectItem = ({ d, field, autoFocus, onChange, labelWidth, fieldWidth, onChangeField, hasChanges, valuesChanged, values, setChanges, alwaysAccept, parsedObjArray, }) => {
    const route = (0, kabouter_1.useRoute)('[object]');
    const object = route.query.object;
    const [open, setOpen] = (0, react_1.useState)(false);
    const splitField = field.split('.');
    const openFunc = () => {
        route.setQuery({
            object: field.split('.').join('.'),
        });
        setOpen(true);
    };
    (0, react_1.useEffect)(() => {
        const objectStr = object;
        if (open) {
            const thingy = objectStr.split('.');
            thingy.pop();
            const boolArr = thingy
                .filter((_, i) => i < splitField.length)
                .map((v, i) => v === splitField[i]);
            if (boolArr.includes(false) || boolArr.length < splitField.length - 1) {
                setOpen(false);
            }
            if (objectStr === '') {
                setOpen(false);
            }
        }
    }, [object]);
    const breadCrumbData = splitField.map((item, i) => {
        return [
            field
                .split('.')
                .filter((_, index) => index <= i)
                .join('.'),
            item,
        ];
    });
    const closeFunc = () => {
        route.setQuery({ object: splitField.slice(0, -1).join('.') });
    };
    return (react_1.default.createElement(components_1.SidePanel.Root, { open: open },
        react_1.default.createElement(components_1.SidePanel.Trigger, null,
            react_1.default.createElement(components_1.Button, { onClick: openFunc, size: "small" },
                "Open Overlay: ",
                field)),
        react_1.default.createElement(components_1.SidePanel.Content, null,
            react_1.default.createElement(react_1.default.Fragment, null,
                react_1.default.createElement(components_1.SidePanel.Title, { closeFunc: closeFunc },
                    react_1.default.createElement(inlines_1.styled.div, { style: {
                            display: 'flex',
                            width: '100%',
                            justifyContent: 'space-between',
                            alignItems: 'center',
                        } },
                        d,
                        react_1.default.createElement(components_1.Breadcrumbs, { onChange: (v) => route.setQuery({ object: v }), data: Object.fromEntries(breadCrumbData) }))),
                react_1.default.createElement(components_1.SidePanel.Body, null, parsedObjArray.map((item) => {
                    return (react_1.default.createElement(Item_1.FormItem, { objValues: item.items, key: item.field, autoFocus: autoFocus, onChangeObj: onChange, fieldWidth: fieldWidth, width: labelWidth, item: {
                            ...item,
                            label: item.field.split('.')[item.field.split('.').length - 1],
                        }, onChange: onChangeField, hasChanges: hasChanges, valuesChanged: valuesChanged, setChanges: setChanges, alwaysAccept: alwaysAccept, value: hasChanges
                            ? (0, utils_1.getValue)(item.field, valuesChanged.current) ??
                                item.value ??
                                (0, utils_1.getValue)(item.field, values)
                            : item.value ?? (0, utils_1.getValue)(item.field, values) }));
                })),
                react_1.default.createElement(components_1.SidePanel.Actions, null,
                    react_1.default.createElement(components_1.Button, { keyboardShortcut: "Esc", onClick: closeFunc }, "Close"),
                    react_1.default.createElement(components_1.Button, { onClick: closeFunc }, "Confirm"))))));
};
exports.ObjectItem = ObjectItem;
//# sourceMappingURL=ObjectItem.js.map