/**
 * amis-ui v6.14.0-beta.1
 * 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 Value = require('./condition-builder/Value.js');
var Select = require('./Select.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;
var MixedInput = function (_a) {
    var _b;
    var value = _a.value, onChange = _a.onChange, methods = _a.methods, hasError = _a.hasError, disabled = _a.disabled, cx = _a.classnames, className = _a.className, style = _a.style, popOverContainer = _a.popOverContainer;
    var _c = tslib.__read(React__default["default"].useState(false), 2), focused = _c[0], setFocused = _c[1];
    var handleFocus = React__default["default"].useCallback(function () {
        setFocused(true);
    }, []);
    var handleBlur = React__default["default"].useCallback(function () {
        setFocused(false);
    }, []);
    var _d = tslib.__read(React__default["default"].useState(null), 2), defaultMethod = _d[0], setDefaultMethod = _d[1];
    var method = React__default["default"].useMemo(function () {
        if (!Array.isArray(methods) || !methods.length) {
            return null;
        }
        return (methods.find(function (m) { return m.test && m.test(value, defaultMethod); }) ||
            defaultMethod ||
            methods[0]);
    }, [value, methods, defaultMethod]);
    var options = React__default["default"].useMemo(function () {
        return methods.map(function (m) {
            var _a;
            return ({
                label: m.label,
                value: (_a = m.value) !== null && _a !== void 0 ? _a : m.label
            });
        });
    }, [methods]);
    var methodChange = React__default["default"].useCallback(function (methodType) {
        var newMethod = methods.find(function (m) { var _a; return ((_a = m.value) !== null && _a !== void 0 ? _a : m.label) === methodType; });
        if (!newMethod) {
            return;
        }
        if (newMethod.convert) {
            // 如果有转换方法，先转换一下
            value = newMethod.convert((method === null || method === void 0 ? void 0 : method.pipeIn) ? method.pipeIn(value) : value, value);
        }
        else {
            value = (method === null || method === void 0 ? void 0 : method.pipeIn) ? method.pipeIn(value) : value; // 如果有转换方法，先转换一下
        }
        setDefaultMethod(newMethod);
        onChange === null || onChange === void 0 ? void 0 : onChange(newMethod.pipeOut ? newMethod.pipeOut(value) : value);
    }, [methods, method, value]);
    var inputValue = React__default["default"].useMemo(function () {
        if (!method) {
            return value;
        }
        return method.pipeIn ? method.pipeIn(value) : value;
    }, [value, method]);
    var inputOnChange = React__default["default"].useCallback(function (newValue) {
        if (method && method.pipeOut) {
            newValue = method.pipeOut(newValue);
        }
        onChange === null || onChange === void 0 ? void 0 : onChange(newValue);
    }, [method]);
    var field = React__default["default"].useMemo(function () {
        if (!method) {
            return {
                type: 'text',
                label: 'whatever',
                name: 'whatever'
            };
        }
        return tslib.__assign(tslib.__assign({}, method.inputSettings), { name: 'whatever', type: (method.type || 'text'), label: method.label });
    }, [method]);
    return (_J$X_("div", { style: style, className: cx("InputGroup MixedInput", className, {
            'is-focused': focused,
            'is-error': hasError,
            'is-disabled': disabled
        }) },
        _J$X_(Value["default"], { className: "MixedInput-input", disabled: disabled, field: field, value: inputValue, onChange: inputOnChange, onFocus: handleFocus, onBlur: handleBlur }),
        _J$X_("div", { className: cx('MixedInput-select') },
            _J$X_(Select["default"], { className: cx('MixedInput-select'), value: method ? (_b = method.value) !== null && _b !== void 0 ? _b : method.label : '', options: options, disabled: disabled, simpleValue: true, onChange: methodChange, clearable: false, popOverContainer: popOverContainer }))));
};
var MixedInput$1 = amisCore.themeable(MixedInput);

exports["default"] = MixedInput$1;
