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

import { __assign } from 'tslib';
import React__default, { useCallback } from 'react';
import pick from 'lodash/pick';
import { themeable, localeable, uncontrollable, isExpression } from 'amis-core';
import { SelectWithRemoteOptions } from '../Select.js';
import NumberInput from '../NumberInput.js';
import DatePicker from '../DatePicker.js';
import Tag from '../Tag.js';
import CodeEditor from './CodeEditor.js';
import InputBox from '../InputBox.js';
import omit from 'lodash/omit';

var FormulaInput = function (props, ref) {
    var _a, _b, _c;
    var __ = props.translate, className = props.className, cx = props.classnames, placeholder = props.placeholder, borderMode = props.borderMode, evalMode = props.evalMode; props.mixedMode; var value = props.value, variables = props.variables, functions = props.functions, _d = props.inputSettings, inputSettings = _d === void 0 ? { type: 'text' } : _d, popOverContainer = props.popOverContainer, onChange = props.onChange, customInputRender = props.customInputRender;
    var schemaType = inputSettings.type;
    /** 自上层共享的属性 */
    var sharedProps = pick(props, ['disabled', 'clearable', 'data']);
    var pipInValue = useCallback(function (value) {
        /** 数据来源可能是从 query中下发的（CRUD查询表头），导致数字或者布尔值被转为 string 格式，这里预处理一下 */
        if (schemaType === 'number') {
            value = isNaN(+value) ? value : +value;
        }
        else if (schemaType === 'boolean') {
            value = value === 'true' ? true : value === 'false' ? false : value;
        }
        return value;
    }, [schemaType]);
    var pipOutValue = useCallback(function (origin) {
        var result = origin;
        if (origin === undefined) {
            onChange === null || onChange === void 0 ? void 0 : onChange(result);
            return;
        }
        if (schemaType === 'boolean') {
            result = origin.value;
        }
        else if (schemaType === 'select') {
            var _a = inputSettings.joinValues, joinValues = _a === void 0 ? true : _a, extractValue = inputSettings.extractValue, delimiter = inputSettings.delimiter, multiple = inputSettings.multiple, _b = inputSettings.valueField, valueField_1 = _b === void 0 ? 'value' : _b;
            if (joinValues) {
                if (multiple) {
                    result = Array.isArray(origin)
                        ? origin.map(function (item) { return item[valueField_1]; }).join(delimiter)
                        : origin
                            ? origin[valueField_1]
                            : '';
                }
                else {
                    result = origin ? origin[valueField_1] : '';
                }
            }
            else if (extractValue) {
                if (multiple) {
                    result = Array.isArray(origin)
                        ? origin.map(function (item) { return item[valueField_1]; })
                        : origin
                            ? [origin[valueField_1 || 'value']]
                            : [];
                }
                else {
                    result = origin ? origin[valueField_1] : '';
                }
            }
        }
        onChange === null || onChange === void 0 ? void 0 : onChange(result);
    }, [schemaType, onChange, inputSettings]);
    var cmptValue = pipInValue(value !== null && value !== void 0 ? value : inputSettings.defaultValue);
    var isExpr = isExpression(cmptValue);
    if (!isExpr && schemaType === 'number') {
        return (React__default.createElement(NumberInput, __assign({}, sharedProps, { className: cx(className, 'FormulaPicker-input-number'), borderMode: "none", placeholder: __(placeholder !== null && placeholder !== void 0 ? placeholder : 'NumberInput.placeholder'), step: inputSettings.step, min: inputSettings.minimum, max: inputSettings.maximum, precision: inputSettings.precision, value: cmptValue, onChange: pipOutValue })));
    }
    else if (!isExpr && schemaType === 'date') {
        return (React__default.createElement(DatePicker, __assign({}, sharedProps, { className: cx(className, 'FormulaPicker-input-date'), borderMode: "none", closeOnSelect: true, placeholder: __(placeholder !== null && placeholder !== void 0 ? placeholder : 'Date.placeholder'), format: inputSettings.format || 'YYYY-MM-DD', inputFormat: inputSettings.inputFormat || 'YYYY-MM-DD', timeFormat: "", popOverContainer: popOverContainer, value: cmptValue, onChange: pipOutValue })));
    }
    else if (!isExpr && schemaType === 'time') {
        return (React__default.createElement(DatePicker, __assign({}, sharedProps, { className: cx(className, 'FormulaPicker-input-time'), viewMode: "time", borderMode: "none", closeOnSelect: true, placeholder: __(placeholder !== null && placeholder !== void 0 ? placeholder : 'Time.placeholder'), format: inputSettings.format || 'HH:mm', inputFormat: inputSettings.inputFormat || 'HH:mm', dateFormat: "", timeFormat: inputSettings.format || 'HH:mm', popOverContainer: popOverContainer, value: cmptValue, onChange: pipOutValue })));
    }
    else if (!isExpr && schemaType === 'datetime') {
        return (React__default.createElement(DatePicker, __assign({}, sharedProps, { className: cx(className, 'FormulaPicker-input-datetime'), borderMode: "none", closeOnSelect: true, placeholder: __(placeholder !== null && placeholder !== void 0 ? placeholder : 'Time.placeholder'), format: inputSettings.format || '', inputFormat: inputSettings.inputFormat || 'YYYY-MM-DD HH:mm', timeFormat: inputSettings.timeFormat || 'HH:mm', popOverContainer: popOverContainer, value: cmptValue, onChange: pipOutValue })));
    }
    else if (!isExpr && (schemaType === 'select' || schemaType === 'boolean')) {
        return (React__default.createElement(SelectWithRemoteOptions, __assign({}, sharedProps, { className: cx(className, "FormulaPicker-input-".concat(schemaType)), borderMode: "none", multiple: schemaType === 'boolean' ? false : inputSettings.multiple, options: schemaType === 'boolean'
                ? [
                    {
                        label: __((_a = inputSettings === null || inputSettings === void 0 ? void 0 : inputSettings.trueLabel) !== null && _a !== void 0 ? _a : 'FormulaInput.True'),
                        value: true
                    },
                    {
                        label: __((_b = inputSettings === null || inputSettings === void 0 ? void 0 : inputSettings.falseLabel) !== null && _b !== void 0 ? _b : 'FormulaInput.False'),
                        value: false
                    }
                ]
                : (_c = inputSettings.options) !== null && _c !== void 0 ? _c : [], source: inputSettings.source, value: pipInValue(value), renderValueLabel: function (option) {
                var _a, _b;
                var label = (_b = (_a = option.label) === null || _a === void 0 ? void 0 : _a.toString()) !== null && _b !== void 0 ? _b : '';
                return schemaType === 'boolean' || !inputSettings.multiple ? (React__default.createElement(Tag, { label: label, className: cx('rounded') })) : (React__default.createElement(React__default.Fragment, null, label));
            }, onChange: pipOutValue })));
    }
    else if (!isExpr && schemaType === 'custom' && customInputRender) {
        return customInputRender({
            value: cmptValue,
            onChange: pipOutValue,
            inputSettings: inputSettings,
            className: "FormulaPicker-input-custom"
        });
    }
    else if (!isExpr &&
        schemaType &&
        schemaType != 'text' &&
        customInputRender) {
        return customInputRender({
            value: cmptValue,
            onChange: pipOutValue,
            inputSettings: {
                value: omit(inputSettings, [
                    'label',
                    'operators',
                    'defaultOp',
                    'defaultValue'
                ])
            },
            className: "FormulaPicker-input-custom"
        });
    }
    else {
        return (React__default.createElement(InputBox, { className: cx('FormulaPicker-input'), inputRender: function (_a) {
                var value = _a.value, onChange = _a.onChange, onFocus = _a.onFocus, onBlur = _a.onBlur, placeholder = _a.placeholder;
                return (React__default.createElement(CodeEditor, { singleLine: true, value: value, onChange: onChange, onFocus: onFocus, onBlur: onBlur, functions: functions, variables: variables, evalMode: evalMode, placeholder: placeholder }));
            }, borderMode: borderMode, value: cmptValue, onChange: pipOutValue, placeholder: __(placeholder !== null && placeholder !== void 0 ? placeholder : 'placeholder.enter') }));
    }
};
var FormulaInput$1 = themeable(localeable(uncontrollable(React__default.forwardRef(FormulaInput), {
    value: 'onChange'
})));

export { FormulaInput$1 as default };
