import { jsx } from "react/jsx-runtime";
import { useEffect } from "react";
import { connect, mapProps, mapReadPretty, useForm } from "@tachybase/schema";
import { LoadingOutlined } from "@ant-design/icons";
import { Input } from "antd";
import { customAlphabet as external_nanoid_customAlphabet } from "nanoid";
import { useTranslation } from "react-i18next";
import { useCollectionField } from "../../../data-source/collection-field/CollectionFieldProvider.mjs";
import { ReadPretty } from "../input/index.mjs";
const NanoIDInput = Object.assign(connect(Input, mapProps((props, field)=>{
    const { size, customAlphabet } = useCollectionField() || {};
    const { t } = useTranslation();
    const form = useForm();
    function isValidNanoid(value) {
        if ((null == value ? void 0 : value.length) !== size) return t('Field value size is') + " ".concat(size || 21);
        for(let i = 0; i < value.length; i++)if ((null == customAlphabet ? void 0 : customAlphabet.indexOf(value[i])) === -1) return t("Field value do not meet the requirements");
    }
    useEffect(()=>{
        if (!field.initialValue && customAlphabet) field.setInitialValue(external_nanoid_customAlphabet(customAlphabet, size)());
        form.setFieldState(field.props.name, (state)=>{
            state.validator = isValidNanoid;
        });
    }, []);
    return {
        ...props,
        suffix: /*#__PURE__*/ jsx("span", {
            children: (null == field ? void 0 : field['loading']) || (null == field ? void 0 : field['validating']) ? /*#__PURE__*/ jsx(LoadingOutlined, {}) : props.suffix
        })
    };
}), mapReadPretty(ReadPretty.Input)), {
    ReadPretty: ReadPretty.Input
});
export { NanoIDInput };
