import { defineComponent, inject, ref, computed, onMounted, createBlock, openBlock, resolveDynamicComponent, h, unref } from "vue";
import { inputProps } from "./types.mjs";
import { formItemInjectKey } from "../form/provide.mjs";
import { innerComponentInjectKey } from "../_components/provide.mjs";
import _sfc_main$2 from "../_components/in-input/InInput.vue.mjs";
import _sfc_main$1 from "../_components/in-box/InBox.vue.mjs";
import { uniqueId, pick, formateToString } from "../_utils/helper.mjs";
const _sfc_main = /* @__PURE__ */ defineComponent({
  __name: "OInput",
  props: inputProps,
  emits: ["update:modelValue", "change", "input", "blur", "focus", "clear", "pressEnter"],
  setup(__props, { expose: __expose, emit: __emit }) {
    const props = __props;
    const emits = __emit;
    const innerComponentInject = inject(innerComponentInjectKey, null);
    const formItemInjection = (innerComponentInject == null ? void 0 : innerComponentInject.isInnerInput) ? null : inject(formItemInjectKey, null);
    const inInputRef = ref();
    const color = computed(() => {
      var _a;
      if (formItemInjection == null ? void 0 : formItemInjection.fieldResult.value) {
        return ((_a = formItemInjection == null ? void 0 : formItemInjection.fieldResult.value) == null ? void 0 : _a.type) || "normal";
      }
      return props.color;
    });
    const onInput = (e, value) => {
      var _a, _b;
      emits("input", e, value);
      (_b = formItemInjection == null ? void 0 : (_a = formItemInjection.fieldHandlers).onInput) == null ? void 0 : _b.call(_a);
    };
    const isFocus = ref(false);
    const onFocus = (e) => {
      var _a, _b;
      if (isFocus.value) {
        return;
      }
      isFocus.value = true;
      emits("focus", e);
      (_b = formItemInjection == null ? void 0 : (_a = formItemInjection.fieldHandlers).onFocus) == null ? void 0 : _b.call(_a);
    };
    const onBlur = (e) => {
      var _a, _b;
      isFocus.value = false;
      emits("blur", e);
      (_b = formItemInjection == null ? void 0 : (_a = formItemInjection.fieldHandlers).onBlur) == null ? void 0 : _b.call(_a);
    };
    const onPressEnter = (e) => {
      emits("pressEnter", e);
    };
    const onClear = (e) => {
      emits("clear", e);
    };
    const onUpdatedModelValue = (value) => {
      emits("update:modelValue", value);
    };
    const onChange = (value) => {
      var _a, _b;
      emits("change", value);
      (_b = formItemInjection == null ? void 0 : (_a = formItemInjection.fieldHandlers).onChange) == null ? void 0 : _b.call(_a);
    };
    const inputId = ref(props.inputId);
    onMounted(() => {
      if (!inputId.value) {
        inputId.value = uniqueId();
      }
    });
    __expose({
      focus: () => {
        var _a;
        return (_a = inInputRef.value) == null ? void 0 : _a.focus();
      },
      blur: () => {
        var _a;
        return (_a = inInputRef.value) == null ? void 0 : _a.blur();
      },
      clear: () => {
        var _a;
        return (_a = inInputRef.value) == null ? void 0 : _a.clear();
      },
      inputEl: () => {
        var _a;
        return (_a = inInputRef.value) == null ? void 0 : _a.inputEl;
      },
      togglePassword: () => {
        var _a;
        return (_a = inInputRef.value) == null ? void 0 : _a.togglePassword();
      }
    });
    return (_ctx, _cache) => {
      return openBlock(), createBlock(resolveDynamicComponent(
        h(
          unref(_sfc_main$1),
          {
            class: "o-input",
            size: props.size,
            variant: props.variant,
            color: color.value,
            disabled: props.disabled,
            readonly: props.readonly,
            round: props.round,
            focused: isFocus.value
          },
          {
            default: () => h(
              unref(_sfc_main$2),
              {
                ref: "inInputRef",
                class: [
                  "o-input-wrap",
                  {
                    "has-suffix": _ctx.$slots.suffix,
                    "has-prepend": _ctx.$slots.prepend,
                    "has-append": _ctx.$slots.append
                  }
                ],
                inputId: inputId.value,
                modelValue: unref(formateToString)(props.modelValue),
                defaultValue: unref(formateToString)(props.defaultValue),
                ...unref(pick)(props, [
                  "type",
                  "placeholder",
                  "disabled",
                  "readonly",
                  "clearable",
                  "format",
                  "showPasswordEvent",
                  "validate",
                  "valueOnInvalidChange",
                  "autoWidth",
                  "maxLength",
                  "minLength",
                  "getLength",
                  "inputOnOutlimit"
                ]),
                onChange,
                onInput,
                onFocus,
                onBlur,
                onPressEnter,
                onClear,
                "onUpdate:modelValue": onUpdatedModelValue
              },
              unref(pick)(_ctx.$slots, ["extra", "prefix", "suffix"])
            ),
            ...unref(pick)(_ctx.$slots, ["append", "prepend"])
          }
        )
      ));
    };
  }
});
export {
  _sfc_main as default
};
