"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.default = ObjectFieldTemplate;
exports.getGlyphButtons = getGlyphButtons;
exports.getObjectTemplateClassNames = getObjectTemplateClassNames;
const React = __importStar(require("react"));
const utils_1 = require("@rjsf/utils");
const utils_2 = require("../../utils");
const ArrayFieldTemplate_1 = require("./ArrayFieldTemplate");
const ReactContext_1 = __importDefault(require("../../ReactContext"));
function ObjectFieldTemplate(props) {
    const TitleFieldTemplate = (0, utils_1.getTemplate)("TitleFieldTemplate", props.registry, (0, utils_2.getUiOptions)(props.uiSchema));
    const DescriptionFieldTemplate = (0, utils_1.getTemplate)("DescriptionFieldTemplate", props.registry, (0, utils_2.getUiOptions)(props.uiSchema));
    const { ButtonToolbar } = React.useContext(ReactContext_1.default).theme;
    let buttons = getGlyphButtons(props);
    const [topButtons, bottomButtons, leftButtons, rightButtons] = ["top", "bottom", "left", "right"].map(pos => {
        const buttons = (0, ArrayFieldTemplate_1.getButtonsForPosition)(props, (0, utils_2.getUiOptions)(props.uiSchema).buttons, pos, "no default");
        return buttons
            ? (React.createElement(ButtonToolbar, { key: `buttons-${pos}` }, buttons))
            : null;
    });
    const { containerClassName, schemaClassName, buttonsClassName } = getObjectTemplateClassNames(props, buttons);
    buttons = React.createElement("div", { className: buttonsClassName }, buttons);
    const titleUiSchema = Object.assign(Object.assign({}, (props.uiSchema || {})), { "_ui:renderedButtons": buttons });
    return (React.createElement("div", { className: containerClassName },
        React.createElement("fieldset", { className: schemaClassName },
            props.title &&
                React.createElement(TitleFieldTemplate, { id: `${props.idSchema.$id}__title`, title: props.title, required: props.required || props.uiSchema["ui:required"], schema: props.schema, uiSchema: titleUiSchema, registry: props.registry }),
            topButtons,
            props.description &&
                React.createElement(DescriptionFieldTemplate, { id: `${props.idSchema.$id}__description`, description: props.description, registry: props.registry, schema: props.schema }),
            leftButtons && (React.createElement("div", { className: "pull-left" }, leftButtons)),
            props.properties.map(({ content }) => content),
            rightButtons && (React.createElement("div", { className: "pull-right" }, rightButtons)),
            bottomButtons),
        !props.title && buttons ? buttons : undefined));
}
function getGlyphButtons(props) {
    const { uiSchema } = props;
    let buttons = uiSchema["ui:buttons"] || [];
    const buttonDescriptions = ((0, utils_2.getUiOptions)(uiSchema).buttons || []).filter(buttonDef => !buttonDef.position);
    if (buttonDescriptions) {
        buttons = [
            ...buttons,
            ...buttonDescriptions.map(buttonDescription => (0, ArrayFieldTemplate_1.getButton)(buttonDescription, props))
        ];
    }
    return buttons
        ? props.title
            ? buttons.reverse()
            : buttons
        : null;
}
function getObjectTemplateClassNames(props, buttons) {
    const vertical = props.uiSchema["ui:buttonsVertical"];
    let containerClassName, schemaClassName, buttonsClassName;
    if (buttons && buttons.length) {
        containerClassName = "laji-form-field-template-item" + (vertical ? " keep-vertical" : "");
        schemaClassName = "laji-form-field-template-schema";
        buttonsClassName = "laji-form-field-template-buttons";
        if (props.title)
            buttonsClassName += " pull-right";
    }
    return { containerClassName, schemaClassName, buttonsClassName };
}
