/**
 * amis v3.5.1
 * build time: 2023-11-02
 * Copyright 2018-2023 baidu
 */

import { __extends, __assign, __decorate } from 'tslib';
import React from 'react';
import { getPropValue, resolveVariableAndFilter, isObject, isPureVariable, buildStyle, Renderer, createObject } from 'amis-core';

function EachItem(props) {
    var render = props.render, data = props.data, items = props.items, item = props.item, name = props.name, index = props.index, itemKeyName = props.itemKeyName, indexKeyName = props.indexKeyName;
    var ctx = React.useMemo(function () {
        var _a;
        return createObject(data, __assign(__assign({}, (isObject(item) ? item : {})), (_a = {}, _a[name] = item, _a[itemKeyName || 'item'] = item, _a[indexKeyName || 'index'] = index, _a)));
    }, [item, data, name, index, itemKeyName, indexKeyName]);
    return render("item/".concat(index), items, {
        data: ctx
    });
}
var Each = /** @class */ (function (_super) {
    __extends(Each, _super);
    function Each() {
        return _super !== null && _super.apply(this, arguments) || this;
    }
    Each.prototype.render = function () {
        var _this = this;
        var _a = this.props, data = _a.data, name = _a.name, className = _a.className, style = _a.style, render = _a.render, items = _a.items, itemKeyName = _a.itemKeyName, indexKeyName = _a.indexKeyName, placeholder = _a.placeholder, cx = _a.classnames, __ = _a.translate;
        var value = getPropValue(this.props, function (props) {
            return props.source
                ? resolveVariableAndFilter(props.source, props.data, '| raw')
                : undefined;
        });
        var arr = isObject(value)
            ? Object.keys(value).map(function (key) { return ({
                key: key,
                value: value[key]
            }); })
            : Array.isArray(value)
                ? value
                : [];
        // 最大循环次数支持
        var maxLength = isPureVariable(this.props.maxLength)
            ? resolveVariableAndFilter(this.props.maxLength, this.props.data) || 0
            : this.props.maxLength;
        if (Array.isArray(arr) && maxLength >= 1 && arr.length > maxLength) {
            arr = arr.slice(0, maxLength);
        }
        return (React.createElement("div", { className: cx('Each', className), style: buildStyle(style, data) }, Array.isArray(arr) && arr.length && items ? (arr.map(function (item, index) { return (React.createElement(EachItem, __assign({}, _this.props, { items: items, key: index, index: index, data: data, item: item, name: name, itemKeyName: itemKeyName, indexKeyName: indexKeyName }))); })) : (React.createElement("div", { className: cx('Each-placeholder') }, render('placeholder', __(placeholder))))));
    };
    Each.propsList = ['name', 'items', 'value'];
    Each.defaultProps = {
        className: '',
        placeholder: 'placeholder.noData'
    };
    return Each;
}(React.Component));
/** @class */ ((function (_super) {
    __extends(EachRenderer, _super);
    function EachRenderer() {
        return _super !== null && _super.apply(this, arguments) || this;
    }
    EachRenderer = __decorate([
        Renderer({
            type: 'each'
        })
    ], EachRenderer);
    return EachRenderer;
})(Each));

export { Each as default };
window.amisVersionInfo={version:'3.5.1',buildTime:'2023-11-02'};
