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

import { __extends, __awaiter, __generator, __assign, __decorate, __metadata } from 'tslib';
import React__default from 'react';
import { autobind } from 'amis-core';
import Button from '../Button.js';
import Checkbox from '../Checkbox.js';
import Form from '../Form.js';
import { Controller } from '../FormField.js';
import { Icon } from '../icons.js';
import InputBox from '../InputBox.js';
import PickerContainer from '../PickerContainer.js';
import EnhancedSelect from '../Select.js';
import Textarea from '../Textarea.js';
import { SchemaEditorItem } from './Item.js';

var schemaEditorItemPlaceholder = {
    key: 'JSONSchema.key',
    title: 'JSONSchema.title',
    description: 'JSONSchema.description',
    default: 'JSONSchema.default',
    empty: 'placeholder.empty'
};
var SchemaEditorItemCommon = /** @class */ (function (_super) {
    __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 __awaiter(this, void 0, void 0, function () {
            var _a, onFormConfirm, onChange, value;
            return __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(__assign(__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 (React__default.createElement(React__default.Fragment, null,
            prefix,
            mini ? (React__default.createElement("span", { className: cx('SchemaEditor-info') },
                React__default.createElement("span", { className: cx('SchemaEditor-label') },
                    (value === null || value === void 0 ? void 0 : value.isRequired) ? (React__default.createElement("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) || '')),
                React__default.createElement("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 ? (React__default.createElement(EnhancedSelect, { 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 ? (React__default.createElement(Checkbox, { 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 ? (React__default.createElement(PickerContainer, { mobileUI: mobileUI, value: value, bodyRender: function (_a) {
                    var isOpened = _a.isOpened, value = _a.value, onChange = _a.onChange, ref = _a.ref;
                    return isOpened ? (React__default.createElement(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 (React__default.createElement(Button, { disabled: disabled || !!(value === null || value === void 0 ? void 0 : value.$ref), className: cx('SchemaEditor-btn'), onClick: onClick, level: mini ? 'link' : 'default' },
                    React__default.createElement(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 = __assign(__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 (React__default.createElement(Form, { defaultValue: value, onChange: this.handleFormChange, onSubmit: onChange, ref: formRef }, function (methods) {
            var _a, _b;
            return (React__default.createElement(React__default.Fragment, null, (_a = formPrefixRender === null || formPrefixRender === void 0 ? void 0 : formPrefixRender(methods)) !== null && _a !== void 0 ? _a : null,
                mini ? (React__default.createElement(Controller, { label: __('JSONSchema.type'), name: "type", control: methods.control, rules: { maxLength: 20 }, isRequired: true, render: function (_a) {
                        var field = _a.field;
                        return (React__default.createElement(EnhancedSelect, __assign({}, field, { block: true, options: types, className: cx('SchemaEditor-type'), clearable: false, disabled: disabled, simpleValue: true, mobileUI: mobileUI, popOverContainer: popOverContainer })));
                    } })) : null,
                React__default.createElement(Controller, { label: __('JSONSchema.title'), name: "title", control: methods.control, rules: { maxLength: 20 }, render: function (_a) {
                        var _b;
                        var field = _a.field;
                        return (React__default.createElement(InputBox, __assign({}, field, { disabled: disabled, placeholder: __((_b = placeholder === null || placeholder === void 0 ? void 0 : placeholder.title) !== null && _b !== void 0 ? _b : ''), mobileUI: mobileUI })));
                    } }),
                React__default.createElement(Controller, { label: __('JSONSchema.description'), name: "description", control: methods.control, render: function (_a) {
                        var _b;
                        var field = _a.field;
                        return (React__default.createElement(Textarea, __assign({}, field, { disabled: disabled, mobileUI: mobileUI, placeholder: __((_b = placeholder === null || placeholder === void 0 ? void 0 : placeholder.description) !== null && _b !== void 0 ? _b : '') })));
                    } }),
                React__default.createElement(Controller, { label: __('JSONSchema.default'), name: "default", control: methods.control, render: function (_a) {
                        var _b;
                        var field = _a.field;
                        return (React__default.createElement(InputBox, __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 (React__default.createElement("div", { className: cx('SchemaEditorItem', {
                'SchemaEditorItem--mini': mini
            }), "data-amis-name": this.props.dataName }, this.renderCommon()));
    };
    __decorate([
        autobind,
        __metadata("design:type", Function),
        __metadata("design:paramtypes", [Object]),
        __metadata("design:returntype", void 0)
    ], SchemaEditorItemCommon.prototype, "handleTypeChange", null);
    __decorate([
        autobind,
        __metadata("design:type", Function),
        __metadata("design:paramtypes", [Object]),
        __metadata("design:returntype", Promise)
    ], SchemaEditorItemCommon.prototype, "handlePropsChange", null);
    __decorate([
        autobind,
        __metadata("design:type", Function),
        __metadata("design:paramtypes", [Object]),
        __metadata("design:returntype", void 0)
    ], SchemaEditorItemCommon.prototype, "handleBeforeSubmit", null);
    __decorate([
        autobind,
        __metadata("design:type", Function),
        __metadata("design:paramtypes", [Object]),
        __metadata("design:returntype", void 0)
    ], SchemaEditorItemCommon.prototype, "handleFormChange", null);
    return SchemaEditorItemCommon;
}(React__default.Component));
var ITEMMAP = {};

export { ITEMMAP, SchemaEditorItemCommon, schemaEditorItemPlaceholder };
