/**
 * 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 pick = require('lodash/pick');
var amisCore = require('amis-core');
var Select = require('../Select.js');
var NumberInput = require('../NumberInput.js');
var DatePicker = require('../DatePicker.js');
var Tag = require('../Tag.js');
var CodeEditor = require('./CodeEditor.js');
var InputBox = require('../InputBox.js');

function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

var React__default = /*#__PURE__*/_interopDefaultLegacy(React);
var pick__default = /*#__PURE__*/_interopDefaultLegacy(pick);

var __react_jsx__ = require('react');
var _J$X_ = (__react_jsx__["default"] || __react_jsx__).createElement;
(__react_jsx__["default"] || __react_jsx__).Fragment;
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__default["default"](props, ['disabled', 'clearable', 'data']);
    var pipInValue = React.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 = React.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 = amisCore.isExpression(cmptValue);
    if (!isExpr && schemaType === 'number') {
        return (_J$X_(NumberInput["default"], tslib.__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 (_J$X_(DatePicker["default"], tslib.__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 (_J$X_(DatePicker["default"], tslib.__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 (_J$X_(DatePicker["default"], tslib.__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 (_J$X_(Select.SelectWithRemoteOptions, tslib.__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 ? (_J$X_(Tag["default"], { label: label, className: cx('rounded') })) : (_J$X_(React__default["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 {
        return (_J$X_(InputBox["default"], { className: cx('FormulaPicker-input'), inputRender: function (_a) {
                var value = _a.value, onChange = _a.onChange, onFocus = _a.onFocus, onBlur = _a.onBlur, placeholder = _a.placeholder;
                return (_J$X_(CodeEditor["default"], { 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 = amisCore.themeable(amisCore.localeable(amisCore.uncontrollable(React__default["default"].forwardRef(FormulaInput), {
    value: 'onChange'
})));

exports["default"] = FormulaInput$1;
