/**
 * 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 } from 'amis-core';
import { Icon } from '../icons.js';
import { SchemaEditorItemCommon, ITEMMAP } from './Common.js';
import { SchemaEditorItem } from './Item.js';
import { Controller } from '../FormField.js';

var SchemaEditorItemArray = /** @class */ (function (_super) {
    __extends(SchemaEditorItemArray, _super);
    function SchemaEditorItemArray() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        _this.state = {
            collapsed: false
        };
        return _this;
    }
    SchemaEditorItemArray.prototype.toggleCollapsed = function () {
        this.setState({
            collapsed: !this.state.collapsed
        });
    };
    SchemaEditorItemArray.prototype.handleItemsChange = function (items) {
        var _a, _b;
        var value = __assign(__assign({}, this.props.value), { type: 'array', items: items });
        (_b = (_a = this.props).onChange) === null || _b === void 0 ? void 0 : _b.call(_a, value);
    };
    SchemaEditorItemArray.prototype.renderItems = function () {
        var _a;
        var _b = this.props, cx = _b.classnames, value = _b.value, renderExtraProps = _b.renderExtraProps, renderModalProps = _b.renderModalProps, locale = _b.locale, __ = _b.translate, classPrefix = _b.classPrefix, disabled = _b.disabled, showInfo = _b.showInfo, types = _b.types, onTypeChange = _b.onTypeChange, enableAdvancedSetting = _b.enableAdvancedSetting, popOverContainer = _b.popOverContainer, placeholder = _b.placeholder, mobileUI = _b.mobileUI, mini = _b.mini;
        var items = (value === null || value === void 0 ? void 0 : value.items) || {
            type: 'string'
        };
        return (React__default.createElement("div", { className: cx('SchemaEditorProps SchemaEditorArrayProps', {
                'SchemaEditorProps--depth': showInfo !== false
            }) },
            React__default.createElement(SchemaEditorItem, { dataName: this.props.dataName, types: types, onTypeChange: onTypeChange, label: (_a = types === null || types === void 0 ? void 0 : types.find(function (item) { return item.value === (items === null || items === void 0 ? void 0 : items.type); })) === null || _a === void 0 ? void 0 : _a.label, prefix: React__default.createElement("div", { className: cx('SchemaEditor-itemsLabel') }, __('JSONSchema.array_items')), value: items, onChange: this.handleItemsChange, renderExtraProps: renderExtraProps, renderModalProps: renderModalProps, locale: locale, translate: __, classnames: cx, classPrefix: classPrefix, disabled: disabled || !!(items === null || items === void 0 ? void 0 : items.$ref), enableAdvancedSetting: enableAdvancedSetting, popOverContainer: popOverContainer, placeholder: placeholder, mobileUI: mobileUI, mini: mini })));
    };
    SchemaEditorItemArray.prototype.render = function () {
        var _a = this.props, cx = _a.classnames, showInfo = _a.showInfo, formMode = _a.formMode, mini = _a.mini; _a.disabled; var mobileUI = _a.mobileUI, locale = _a.locale, classPrefix = _a.classPrefix, types = _a.types, __ = _a.translate, placeholder = _a.placeholder;
        if (formMode) {
            return this.renderForm({
                formAffixRender: function (methods) {
                    return (React__default.createElement(React__default.Fragment, null,
                        React__default.createElement(Controller, { label: __('JSONSchema.array_items'), name: "items", control: methods.control, render: function (_a) {
                                var field = _a.field;
                                return (React__default.createElement(SchemaEditorItem, __assign({}, field, { types: types, placeholder: placeholder, mobileUI: mobileUI, locale: locale, translate: __, classnames: cx, classPrefix: classPrefix, mini: false })));
                            } })));
                }
            });
        }
        return (React__default.createElement("div", { className: cx('SchemaEditorItem SchemaEditorArray', {
                'SchemaEditorItem--mini': mini
            }), "data-amis-name": this.props.dataName },
            showInfo !== false ? (React__default.createElement(React__default.Fragment, null,
                mini ? null : (React__default.createElement("a", { className: cx('SchemaEditor-caret', {
                        'is-collapsed': this.state.collapsed
                    }), onClick: this.toggleCollapsed },
                    React__default.createElement(Icon, { icon: "caret", className: "icon" }))),
                this.renderCommon())) : null,
            this.state.collapsed || mini ? null : this.renderItems()));
    };
    __decorate([
        autobind,
        __metadata("design:type", Function),
        __metadata("design:paramtypes", []),
        __metadata("design:returntype", void 0)
    ], SchemaEditorItemArray.prototype, "toggleCollapsed", null);
    __decorate([
        autobind,
        __metadata("design:type", Function),
        __metadata("design:paramtypes", [Object]),
        __metadata("design:returntype", void 0)
    ], SchemaEditorItemArray.prototype, "handleItemsChange", null);
    return SchemaEditorItemArray;
}(SchemaEditorItemCommon));
ITEMMAP.array = SchemaEditorItemArray;

export { SchemaEditorItemArray };
