/**
 * amis-ui v6.12.0-beta.0
 * Copyright 2018-2025 fex
 */

import { __extends, __assign, __decorate, __metadata } from 'tslib';
import React__default from 'react';
import { autobind, themeable, localeable, uncontrollable } from 'amis-core';
import { SchemaEditorItem } from './Item.js';
import { schemaEditorItemPlaceholder } from './Common.js';
import './Array.js';
import './Object.js';

/**
 * 用来定义数据结构的编辑器
 */
var SchemaEditor = /** @class */ (function (_super) {
    __extends(SchemaEditor, _super);
    function SchemaEditor(props) {
        var _this = _super.call(this, props) || this;
        var __ = props.translate;
        _this.defaultTypes = [
            {
                label: __('SchemaType.string'),
                value: 'string'
            },
            {
                label: __('SchemaType.number'),
                value: 'number'
            },
            {
                label: __('SchemaType.integer'),
                value: 'integer'
            },
            {
                label: __('SchemaType.boolean'),
                value: 'boolean'
            },
            {
                label: __('SchemaType.null'),
                value: 'null'
            },
            {
                label: __('SchemaType.object'),
                value: 'object'
            },
            {
                label: __('SchemaType.array'),
                value: 'array'
            }
        ];
        return _this;
    }
    SchemaEditor.prototype.handleTypeChange = function (type, value, origin) {
        var definitions = this.props.definitions;
        if (type === 'array') {
            value.items = {
                type: 'string'
            };
        }
        if (definitions === null || definitions === void 0 ? void 0 : definitions[type]) {
            value = __assign(__assign(__assign({}, value), definitions[type]), { $ref: type });
        }
        return value;
    };
    SchemaEditor.prototype.render = function () {
        var _a = this.props, defaultType = _a.defaultType, cx = _a.classnames, onChange = _a.onChange, renderExtraProps = _a.renderExtraProps, renderModalProps = _a.renderModalProps, translate = _a.translate, locale = _a.locale, classPrefix = _a.classPrefix, rootTypeMutable = _a.rootTypeMutable, showRootInfo = _a.showRootInfo, disabled = _a.disabled, definitions = _a.definitions, enableAdvancedSetting = _a.enableAdvancedSetting, popOverContainer = _a.popOverContainer, placeholder = _a.placeholder, mobileUI = _a.mobileUI, mini = _a.mini, className = _a.className, addButtonText = _a.addButtonText;
        var value = this.props.value || {
            type: defaultType || 'object'
        };
        var disabledTypes = Array.isArray(this.props.disabledTypes)
            ? this.props.disabledTypes
            : [];
        var types = this.defaultTypes.concat();
        if (definitions) {
            var keys = Object.keys(definitions);
            keys.forEach(function (key) {
                var definition = definitions[key];
                if ((definition === null || definition === void 0 ? void 0 : definition.type) &&
                    definition.title &&
                    [
                        'string',
                        'number',
                        'integer',
                        'object',
                        'array',
                        'boolean',
                        'null'
                    ].includes(definition.type)) {
                    types.push({
                        value: key,
                        label: translate(definition.title)
                    });
                }
            });
        }
        if (disabledTypes.length) {
            types = types.filter(function (item) { return !~disabledTypes.indexOf(item.value); });
        }
        return (React__default.createElement("div", { className: cx('SchemaEditor', className) },
            React__default.createElement(SchemaEditorItem, { types: types, typeMutable: rootTypeMutable, showInfo: showRootInfo, value: value, onChange: onChange, renderExtraProps: renderExtraProps, renderModalProps: renderModalProps, locale: locale, translate: translate, classnames: cx, classPrefix: classPrefix, disabled: disabled, onTypeChange: this.handleTypeChange, enableAdvancedSetting: enableAdvancedSetting, popOverContainer: popOverContainer, placeholder: placeholder, mobileUI: mobileUI, mini: mini, addButtonText: addButtonText, expandMembers: true })));
    };
    SchemaEditor.defaultProps = {
        defaultType: 'object',
        rootTypeMutable: false,
        showRootInfo: false,
        disabledTypes: ['null'],
        placeholder: schemaEditorItemPlaceholder
    };
    __decorate([
        autobind,
        __metadata("design:type", Function),
        __metadata("design:paramtypes", [String, Object, Object]),
        __metadata("design:returntype", void 0)
    ], SchemaEditor.prototype, "handleTypeChange", null);
    return SchemaEditor;
}(React__default.Component));
var JSONSchemaEditor = themeable(localeable(uncontrollable(SchemaEditor, {
    value: 'onChange'
})));

export { SchemaEditor, JSONSchemaEditor as default };
