/**
 * 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 Button = require('../Button.js');
var Checkbox = require('../Checkbox.js');
var Form = require('../Form.js');
var FormField = require('../FormField.js');
var icons = require('../icons.js');
var InputBox = require('../InputBox.js');
var PickerContainer = require('../PickerContainer.js');
var Select = require('../Select.js');
var Textarea = require('../Textarea.js');
var Item = require('./Item.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 schemaEditorItemPlaceholder = {
    key: 'JSONSchema.key',
    title: 'JSONSchema.title',
    description: 'JSONSchema.description',
    default: 'JSONSchema.default',
    empty: 'placeholder.empty'
};
var SchemaEditorItemCommon = /** @class */ (function (_super) {
    tslib.__extends(SchemaEditorItemCommon, _super);
    function SchemaEditorItemCommon() {
        return _super !== null && _super.apply(this, arguments) || this;
    }
    SchemaEditorItemCommon.prototype.handleTypeChange = function (type) {
        var _a;
        var _b = this.props, value = _b.value, onChange = _b.onChange, onTypeChange = _b.onTypeChange;
        var newValue = {
            type: type,
            title: value === null || value === void 0 ? void 0 : value.title
        };
        newValue = (_a = onTypeChange === null || onTypeChange === void 0 ? void 0 : onTypeChange(type, newValue, value)) !== null && _a !== void 0 ? _a : newValue;
        onChange === null || onChange === void 0 ? void 0 : onChange(newValue);
    };
    SchemaEditorItemCommon.prototype.handlePropsChange = function (newValue) {
        return tslib.__awaiter(this, void 0, void 0, function () {
            var _a, onFormConfirm, onChange, value;
            return tslib.__generator(this, function (_b) {
                switch (_b.label) {
                    case 0:
                        _a = this.props, onFormConfirm = _a.onFormConfirm, onChange = _a.onChange, value = _a.value;
                        if (!onFormConfirm) return [3 /*break*/, 2];
                        return [4 /*yield*/, onFormConfirm(newValue)];
                    case 1:
                        _b.sent();
                        _b.label = 2;
                    case 2:
                        onChange === null || onChange === void 0 ? void 0 : onChange(tslib.__assign(tslib.__assign({}, value), newValue));
                        return [2 /*return*/];
                }
            });
        });
    };
    SchemaEditorItemCommon.prototype.handleBeforeSubmit = function (form) {
        return form.submit();
    };
    SchemaEditorItemCommon.prototype.renderCommon = function () {
        var _this = this;
        var _a;
        var _b = this.props, label = _b.label, value = _b.value, __ = _b.translate, locale = _b.locale, typeMutable = _b.typeMutable, disabled = _b.disabled, cx = _b.classnames, classPrefix = _b.classPrefix, required = _b.required, onRequiredChange = _b.onRequiredChange, renderExtraProps = _b.renderExtraProps, renderModalProps = _b.renderModalProps, enableAdvancedSetting = _b.enableAdvancedSetting, popOverContainer = _b.popOverContainer, prefix = _b.prefix, affix = _b.affix, types = _b.types, placeholder = _b.placeholder, mobileUI = _b.mobileUI, mini = _b.mini, formPrefixRender = _b.formPrefixRender, formAffixRender = _b.formAffixRender;
        return (_J$X_(React__default["default"].Fragment, null,
            prefix,
            mini ? (_J$X_("span", { className: cx('SchemaEditor-info') },
                _J$X_("span", { className: cx('SchemaEditor-label') },
                    (value === null || value === void 0 ? void 0 : value.isRequired) ? (_J$X_("span", { className: cx('Form-star') }, "*")) : (''), label !== null && label !== void 0 ? label : ((value === null || value === void 0 ? void 0 : value.title) || (value === null || value === void 0 ? void 0 : value.key) || '')),
                _J$X_("span", { className: cx('SchemaEditor-typeLabel') }, ((_a = types.find(function (item) { return item.value === (value === null || value === void 0 ? void 0 : value.type); })) === null || _a === void 0 ? void 0 : _a.label) || ''))) : null,
            !mini && types.length > 1 ? (_J$X_(Select["default"], { options: types, className: cx('SchemaEditor-type'), value: (value === null || value === void 0 ? void 0 : value.$ref) || (value === null || value === void 0 ? void 0 : value.type) || 'string', onChange: this.handleTypeChange, clearable: false, disabled: disabled || typeMutable === false, simpleValue: true, mobileUI: mobileUI, popOverContainer: popOverContainer, dataName: onRequiredChange ? "".concat(this.props.dataName, "-type") : '' })) : null,
            !mini && onRequiredChange ? (_J$X_(Checkbox["default"], { className: cx('SchemaEditor-required'), label: __('Required'), value: required, onChange: onRequiredChange, disabled: disabled, dataName: "".concat(this.props.dataName, "-required") })) : null, renderExtraProps === null || renderExtraProps === void 0 ? void 0 :
            renderExtraProps(value, this.handlePropsChange),
            enableAdvancedSetting || mini ? (_J$X_(PickerContainer["default"], { mobileUI: mobileUI, value: value, bodyRender: function (_a) {
                    var isOpened = _a.isOpened, value = _a.value, onChange = _a.onChange, ref = _a.ref;
                    return isOpened ? (_J$X_(Item.SchemaEditorItem, { types: types, value: value, onChange: onChange, renderExtraProps: renderExtraProps, renderModalProps: renderModalProps, locale: locale, translate: __, classnames: cx, classPrefix: classPrefix, disabled: disabled, onTypeChange: _this.handleTypeChange, enableAdvancedSetting: enableAdvancedSetting, popOverContainer: popOverContainer, placeholder: placeholder, mobileUI: mobileUI, mini: mini, formRef: ref, formMode: true, formPrefixRender: formPrefixRender, formAffixRender: formAffixRender })) : null;
                }, beforeConfirm: this.handleBeforeSubmit, onConfirm: this.handlePropsChange, title: __('SubForm.editDetail'), popOverContainer: popOverContainer }, function (_a) {
                var onClick = _a.onClick;
                return (_J$X_(Button["default"], { disabled: disabled || !!(value === null || value === void 0 ? void 0 : value.$ref), className: cx('SchemaEditor-btn'), onClick: onClick, level: mini ? 'link' : 'default' },
                    _J$X_(icons.Icon, { icon: mini ? 'edit' : 'setting', className: "icon" })));
            })) : null,
            affix));
    };
    SchemaEditorItemCommon.prototype.handleFormChange = function (values) {
        var _a = this.props, originValue = _a.value, onChange = _a.onChange;
        if ((values === null || values === void 0 ? void 0 : values.type) !== (originValue === null || originValue === void 0 ? void 0 : originValue.type)) {
            onChange(values);
        }
    };
    SchemaEditorItemCommon.prototype.renderForm = function (props) {
        var _a = tslib.__assign(tslib.__assign({}, this.props), props), value = _a.value, onChange = _a.onChange, formRef = _a.formRef, __ = _a.translate, disabled = _a.disabled, placeholder = _a.placeholder, mobileUI = _a.mobileUI, renderModalProps = _a.renderModalProps, mini = _a.mini, types = _a.types, cx = _a.classnames, popOverContainer = _a.popOverContainer, formPrefixRender = _a.formPrefixRender, formAffixRender = _a.formAffixRender;
        return (_J$X_(Form["default"], { defaultValue: value, onChange: this.handleFormChange, onSubmit: onChange, ref: formRef }, function (methods) {
            var _a, _b;
            return (_J$X_(React__default["default"].Fragment, null, (_a = formPrefixRender === null || formPrefixRender === void 0 ? void 0 : formPrefixRender(methods)) !== null && _a !== void 0 ? _a : null,
                mini ? (_J$X_(FormField.Controller, { label: __('JSONSchema.type'), name: "type", control: methods.control, rules: { maxLength: 20 }, isRequired: true, render: function (_a) {
                        var field = _a.field;
                        return (_J$X_(Select["default"], tslib.__assign({}, field, { block: true, options: types, className: cx('SchemaEditor-type'), clearable: false, disabled: disabled, simpleValue: true, mobileUI: mobileUI, popOverContainer: popOverContainer })));
                    } })) : null,
                _J$X_(FormField.Controller, { label: __('JSONSchema.title'), name: "title", control: methods.control, rules: { maxLength: 20 }, render: function (_a) {
                        var _b;
                        var field = _a.field;
                        return (_J$X_(InputBox["default"], tslib.__assign({}, field, { disabled: disabled, placeholder: __((_b = placeholder === null || placeholder === void 0 ? void 0 : placeholder.title) !== null && _b !== void 0 ? _b : ''), mobileUI: mobileUI })));
                    } }),
                _J$X_(FormField.Controller, { label: __('JSONSchema.description'), name: "description", control: methods.control, render: function (_a) {
                        var _b;
                        var field = _a.field;
                        return (_J$X_(Textarea["default"], tslib.__assign({}, field, { disabled: disabled, mobileUI: mobileUI, placeholder: __((_b = placeholder === null || placeholder === void 0 ? void 0 : placeholder.description) !== null && _b !== void 0 ? _b : '') })));
                    } }),
                _J$X_(FormField.Controller, { label: __('JSONSchema.default'), name: "default", control: methods.control, render: function (_a) {
                        var _b;
                        var field = _a.field;
                        return (_J$X_(InputBox["default"], tslib.__assign({}, field, { disabled: disabled, placeholder: __((_b = placeholder === null || placeholder === void 0 ? void 0 : placeholder.default) !== null && _b !== void 0 ? _b : ''), mobileUI: mobileUI })));
                    } }), (_b = formAffixRender === null || formAffixRender === void 0 ? void 0 : formAffixRender(methods)) !== null && _b !== void 0 ? _b : null, renderModalProps === null || renderModalProps === void 0 ? void 0 :
                renderModalProps(methods.getValues(), function (values) {
                    Object.keys(values).forEach(function (key) {
                        return methods.setValue(key, values[key]);
                    });
                })));
        }));
    };
    SchemaEditorItemCommon.prototype.render = function () {
        var _a = this.props, cx = _a.classnames, formMode = _a.formMode, mini = _a.mini;
        if (formMode) {
            return this.renderForm();
        }
        return (_J$X_("div", { className: cx('SchemaEditorItem', {
                'SchemaEditorItem--mini': mini
            }), "data-amis-name": this.props.dataName }, this.renderCommon()));
    };
    tslib.__decorate([
        amisCore.autobind,
        tslib.__metadata("design:type", Function),
        tslib.__metadata("design:paramtypes", [Object]),
        tslib.__metadata("design:returntype", void 0)
    ], SchemaEditorItemCommon.prototype, "handleTypeChange", null);
    tslib.__decorate([
        amisCore.autobind,
        tslib.__metadata("design:type", Function),
        tslib.__metadata("design:paramtypes", [Object]),
        tslib.__metadata("design:returntype", Promise)
    ], SchemaEditorItemCommon.prototype, "handlePropsChange", null);
    tslib.__decorate([
        amisCore.autobind,
        tslib.__metadata("design:type", Function),
        tslib.__metadata("design:paramtypes", [Object]),
        tslib.__metadata("design:returntype", void 0)
    ], SchemaEditorItemCommon.prototype, "handleBeforeSubmit", null);
    tslib.__decorate([
        amisCore.autobind,
        tslib.__metadata("design:type", Function),
        tslib.__metadata("design:paramtypes", [Object]),
        tslib.__metadata("design:returntype", void 0)
    ], SchemaEditorItemCommon.prototype, "handleFormChange", null);
    return SchemaEditorItemCommon;
}(React__default["default"].Component));
var ITEMMAP = {};

exports.ITEMMAP = ITEMMAP;
exports.SchemaEditorItemCommon = SchemaEditorItemCommon;
exports.schemaEditorItemPlaceholder = schemaEditorItemPlaceholder;
