/**
 * amis-ui v6.14.0-beta.1
 * Copyright 2018-2025 fex
 */

'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var tslib = require('tslib');
var React = require('react');
var amisCore = require('amis-core');
var Item = require('./Item.js');
var Common = require('./Common.js');
require('./Array.js');
require('./Object.js');

function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

var React__default = /*#__PURE__*/_interopDefaultLegacy(React);

var __react_jsx__ = require('react');
var _J$X_ = (__react_jsx__["default"] || __react_jsx__).createElement;
(__react_jsx__["default"] || __react_jsx__).Fragment;
var SchemaEditor = /** @class */ (function (_super) {
    tslib.__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 = tslib.__assign(tslib.__assign(tslib.__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 (_J$X_("div", { className: cx('SchemaEditor', className) },
            _J$X_(Item.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: Common.schemaEditorItemPlaceholder
    };
    tslib.__decorate([
        amisCore.autobind,
        tslib.__metadata("design:type", Function),
        tslib.__metadata("design:paramtypes", [String, Object, Object]),
        tslib.__metadata("design:returntype", void 0)
    ], SchemaEditor.prototype, "handleTypeChange", null);
    return SchemaEditor;
}(React__default["default"].Component));
var JSONSchemaEditor = amisCore.themeable(amisCore.localeable(amisCore.uncontrollable(SchemaEditor, {
    value: 'onChange'
})));

exports.SchemaEditor = SchemaEditor;
exports["default"] = JSONSchemaEditor;
