/**
 * amis-ui v6.12.0-beta.0
 * 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 icons = require('../icons.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;
function InputJSONSchemaArray(props) {
    var _a, _b, _c;
    var cx = props.classnames, value = props.value, onChange = props.onChange, disabled = props.disabled, __ = props.translate, collapsable = props.collapsable, renderValue = props.renderValue, mobileUI = props.mobileUI;
    var buildMembers = React__default["default"].useCallback(function (schema, value) {
        var members = [];
        var len = Array.isArray(value) ? value.length : 1;
        if (typeof schema.minContains === 'number') {
            len = Math.max(len, schema.minContains);
        }
        var maxContains = typeof schema.maxContains === 'number' ? schema.maxContains : 0;
        while (len--) {
            members.push({
                key: amisCore.guid(),
                index: members.length,
                schema: schema.items,
                invalid: maxContains ? maxContains < members.length : false
            });
        }
        return members;
    }, []);
    var _d = tslib.__read(React__default["default"].useState(buildMembers(props.schema, value)), 2), members = _d[0], setMembers = _d[1];
    var membersRef = React__default["default"].useRef(members);
    membersRef.current = members;
    var _e = tslib.__read(React__default["default"].useState(collapsable ? true : false), 2), collapsed = _e[0], setCollapsed = _e[1];
    var toggleCollapsed = function () {
        setCollapsed(!collapsed);
    };
    var onMemberChange = function (member, memberValue) {
        var newValue = Array.isArray(props.value) ? props.value.concat() : [];
        newValue[member.index] = memberValue;
        onChange === null || onChange === void 0 ? void 0 : onChange(newValue);
    };
    var onMemberDelete = function (member) {
        var idx = members.indexOf(member);
        if (!~idx) {
            throw new Error('member object not found');
        }
        var m = members.concat();
        m.splice(idx, 1);
        setMembers(m);
        var newValue = Array.isArray(props.value) ? props.value.concat() : [];
        newValue.splice(member.index, 1);
        onChange === null || onChange === void 0 ? void 0 : onChange(newValue);
    };
    React__default["default"].useEffect(function () {
        setMembers(buildMembers(props.schema, props.value));
    }, [JSON.stringify(props.schema)]);
    React__default["default"].useEffect(function () {
        var value = props.value;
        var schema = props.schema;
        var len = Array.isArray(value) ? value.length : 1;
        if (typeof schema.minContains === 'number') {
            len = Math.max(len, schema.minContains);
        }
        if (typeof schema.maxContains === 'number') {
            len = Math.min(schema.maxContains, len);
        }
        var m = membersRef.current.concat();
        if (m.length !== len) {
            while (m.length !== len) {
                if (m.length > len) {
                    m.pop();
                }
                else {
                    m.push({
                        key: amisCore.guid(),
                        index: m.length,
                        schema: schema.items
                    });
                }
            }
            setMembers(m);
        }
    }, [JSON.stringify(props.value)]);
    var handleAdd = React__default["default"].useCallback(function () {
        var m = members.concat();
        m.push({
            key: amisCore.guid(),
            index: members.length,
            schema: props.schema.items,
            invalid: false
        });
        setMembers(m);
    }, [members]);
    var maxContains = typeof ((_a = props.schema) === null || _a === void 0 ? void 0 : _a.maxContains) === 'number'
        ? props.schema.maxContains
        : 0;
    var minContains = typeof ((_b = props.schema) === null || _b === void 0 ? void 0 : _b.minContains) === 'number'
        ? props.schema.minContains
        : 0;
    // todo additionalProperties 还有其他格式
    var allowAdd = !maxContains || maxContains > members.length;
    var allowDelete = !minContains || minContains < members.length;
    return (_J$X_(React__default["default"].Fragment, null,
        collapsable ? (_J$X_("a", { className: cx('JSONSchemaObject-caret', {
                'is-collapsed': collapsed
            }), onClick: toggleCollapsed },
            _J$X_(icons.Icon, { icon: "right-arrow-bold", className: "icon" }))) : null,
        _J$X_("div", { className: cx('JSONSchemaObject', {
                'is-mobile': mobileUI,
                'is-expanded': collapsable && !collapsed
            }) },
            collapsed ? (renderValue ? (_J$X_(Item["default"], tslib.__assign({}, props, { value: value, onChange: onChange, schema: {
                    type: 'string'
                }, placeholder: (_c = props.schema) === null || _c === void 0 ? void 0 : _c.description }))) : null) : (members.map(function (member) {
                return (_J$X_("div", { key: member.key, className: cx('JSONSchemaMember') },
                    _J$X_("div", { className: cx('JSONSchemaMember-value') },
                        _J$X_(Item["default"], tslib.__assign({}, props, { value: value === null || value === void 0 ? void 0 : value[member.index], onChange: onMemberChange.bind(null, member), schema: member.schema || {
                                type: 'string'
                            }, collapsable: true }))),
                    allowDelete ? (_J$X_(Button["default"], { className: cx('SchemaEditor-btn'), onClick: onMemberDelete.bind(null, member), iconOnly: true, disabled: disabled || !!(value === null || value === void 0 ? void 0 : value.$ref) },
                        _J$X_(icons.Icon, { icon: "remove", className: "icon" }))) : null));
            })),
            !collapsed ? (_J$X_(Button["default"], { level: "link", onClick: handleAdd, size: "xs", disabled: disabled || !allowAdd }, __('JSONSchema.add_prop'))) : null)));
}

exports.InputJSONSchemaArray = InputJSONSchemaArray;
