/**
 * amis-ui v6.14.0-beta.1
 * Copyright 2018-2025 fex
 */

import { __read, __assign } from 'tslib';
import React__default from 'react';
import { themeable } from 'amis-core';
import FieldValue from './condition-builder/Value.js';
import EnhancedSelect from './Select.js';

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 = __read(React__default.useState(false), 2), focused = _c[0], setFocused = _c[1];
    var handleFocus = React__default.useCallback(function () {
        setFocused(true);
    }, []);
    var handleBlur = React__default.useCallback(function () {
        setFocused(false);
    }, []);
    var _d = __read(React__default.useState(null), 2), defaultMethod = _d[0], setDefaultMethod = _d[1];
    var method = React__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.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.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.useMemo(function () {
        if (!method) {
            return value;
        }
        return method.pipeIn ? method.pipeIn(value) : value;
    }, [value, method]);
    var inputOnChange = React__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.useMemo(function () {
        if (!method) {
            return {
                type: 'text',
                label: 'whatever',
                name: 'whatever'
            };
        }
        return __assign(__assign({}, method.inputSettings), { name: 'whatever', type: (method.type || 'text'), label: method.label });
    }, [method]);
    return (React__default.createElement("div", { style: style, className: cx("InputGroup MixedInput", className, {
            'is-focused': focused,
            'is-error': hasError,
            'is-disabled': disabled
        }) },
        React__default.createElement(FieldValue, { className: "MixedInput-input", disabled: disabled, field: field, value: inputValue, onChange: inputOnChange, onFocus: handleFocus, onBlur: handleBlur }),
        React__default.createElement("div", { className: cx('MixedInput-select') },
            React__default.createElement(EnhancedSelect, { 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 = themeable(MixedInput);

export { MixedInput$1 as default };
