import { jsx } from "react/jsx-runtime";
import { useMemo } from "react";
import { connect, mapReadPretty } from "@tachybase/schema";
import { useGlobalVariable } from "../../../application/hooks/useGlobalVariable.mjs";
import { Input } from "../input/index.mjs";
import { Password } from "../password/index.mjs";
import { RawTextArea } from "./RawTextArea.mjs";
import { TextArea } from "./TextArea.mjs";
import { Variable } from "./Variable.mjs";
const useEnvironmentVariableOptions = (scope)=>{
    const environmentVariables = useGlobalVariable('$env');
    return useMemo(()=>{
        if (environmentVariables) return [
            environmentVariables
        ].filter(Boolean);
        return scope;
    }, [
        environmentVariables,
        scope
    ]);
};
const isVariable = (value)=>{
    const regex = /{{.*?}}/;
    return regex.test(value);
};
const TextAreaWithGlobalScope = connect((props)=>{
    const { supportsLineBreak, password, number, boolean, ...others } = props;
    const scope = useEnvironmentVariableOptions(props.scope);
    const fieldNames = {
        value: 'name',
        label: 'title'
    };
    if (supportsLineBreak) return /*#__PURE__*/ jsx(RawTextArea, {
        ...others,
        scope: scope,
        fieldNames: fieldNames,
        rows: 3
    });
    if (number) return /*#__PURE__*/ jsx(Variable.Input, {
        ...props,
        scope: scope,
        fieldNames: fieldNames
    });
    if (password && props.value && !isVariable(props.value)) return /*#__PURE__*/ jsx(Password, {
        ...others,
        autoFocus: true
    });
    if (boolean) return /*#__PURE__*/ jsx(Variable.Input, {
        ...props,
        scope: scope,
        fieldNames: fieldNames
    });
    return /*#__PURE__*/ jsx(TextArea, {
        ...others,
        scope: scope,
        fieldNames: fieldNames
    });
}, mapReadPretty(Input.ReadPretty));
export { TextAreaWithGlobalScope, useEnvironmentVariableOptions };
