/**
 * 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 PickerColumn = require('./PickerColumn.js');

var __react_jsx__ = require('react');
var _J$X_ = (__react_jsx__["default"] || __react_jsx__).createElement;
(__react_jsx__["default"] || __react_jsx__).Fragment;
function fixToArray(data) {
    if (!Array.isArray(data)) {
        return [data];
    }
    return data;
}
var Picker = React.memo(function (props) {
    var title = props.title, labelField = props.labelField, valueField = props.valueField, _a = props.visibleItemCount, visibleItemCount = _a === void 0 ? 5 : _a, _b = props.value, value = _b === void 0 ? [] : _b, _c = props.swipeDuration, swipeDuration = _c === void 0 ? 1000 : _c, _d = props.columns, columns = _d === void 0 ? [] : _d, _e = props.itemHeight, itemHeight = _e === void 0 ? 48 : _e, _f = props.showToolbar, showToolbar = _f === void 0 ? true : _f, _g = props.className, className = _g === void 0 ? '' : _g, _h = props.highlightTxt, highlightTxt = _h === void 0 ? '' : _h, cx = props.classnames, ns = props.classPrefix, __ = props.translate;
    var _columns = fixToArray(columns);
    var _j = tslib.__read(React.useState(fixToArray(props.value === undefined ? props.defaultValue || [] : value)), 2), innerValue = _j[0], setInnerValue = _j[1];
    React.useEffect(function () {
        if (value === innerValue)
            return;
        setInnerValue(fixToArray(value));
    }, [value]);
    var close = function () {
        if (props.onClose) {
            props.onClose(innerValue);
        }
    };
    var confirm = function () {
        if (props.onConfirm) {
            props.onConfirm(innerValue);
        }
    };
    var onChange = function (itemValue, columnIndex, confirm) {
        var nextInnerValue = tslib.__spreadArray([], tslib.__read(innerValue), false);
        nextInnerValue[columnIndex] = itemValue;
        setInnerValue(nextInnerValue);
        if (props.onChange) {
            props.onChange(nextInnerValue, columnIndex, confirm);
        }
    };
    var renderColumnItem = function (item, index) {
        return (_J$X_(PickerColumn["default"], tslib.__assign({}, item, { highlightTxt: highlightTxt, classnames: cx, classPrefix: ns, labelField: labelField || item.labelField, valueField: valueField || item.valueField, itemHeight: itemHeight, swipeDuration: swipeDuration, visibleItemCount: visibleItemCount, value: innerValue[index], onChange: function (val, i, confirm) {
                onChange(val, index, confirm);
            }, key: "column".concat(index) })));
    };
    var wrapHeight = itemHeight * +visibleItemCount;
    var frameStyle = { height: "".concat(itemHeight, "px") };
    var columnsStyle = { height: "".concat(wrapHeight, "px") };
    var maskStyle = {
        backgroundSize: "100% ".concat((wrapHeight - itemHeight) / 2, "px")
    };
    var hasHeader = showToolbar || title;
    return (_J$X_("div", { className: cx(className, 'PickerColumns', 'PickerColumns-popOver') },
        hasHeader && (_J$X_("div", { className: cx('PickerColumns-header') },
            showToolbar && (_J$X_(Button["default"], { className: "PickerColumns-cancel", level: "link", onClick: close }, __('cancel'))),
            title && _J$X_("div", { className: cx('PickerColumns-title') }, title),
            showToolbar && (_J$X_(Button["default"], { className: "PickerColumns-confirm", level: "link", onClick: confirm }, __('confirm'))))),
        _J$X_("div", { className: cx('PickerColumns-columns'), style: columnsStyle },
            _columns.map(function (column, index) {
                return renderColumnItem(column, index);
            }),
            _J$X_("div", { className: cx('PickerColumns-mask'), style: maskStyle }),
            _J$X_("div", { className: cx('PickerColumns-frame'), style: frameStyle }))));
});
var Picker$1 = amisCore.themeable(amisCore.localeable(amisCore.uncontrollable(Picker, {
    value: 'onChange'
})));

exports["default"] = Picker$1;
