"use strict";
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
;/* empty css                                               */
;/* empty css                                                */
;/* empty css                                               */
;/* empty css                                               */
;/* empty css                                                */
const Vue = require("vue");
const formatterUtil = require("../../../utils/formatterUtil.js");
const useCursor = require("../../../utils/use-cursor.js");
;/* empty css            */
const index = require("../../../../node_modules/vant/es/field/index.js");
const _sfc_main = {
  __name: "Field",
  props: {
    modelValue: {
      type: String,
      default: ""
    },
    fieldType: {
      type: String
    },
    disabled: {
      type: Boolean,
      default: false
    },
    readonly: {
      type: Boolean,
      default: false
    },
    clickable: {
      type: Boolean,
      default: false
    },
    dataFormatter: {
      type: String,
      default: ""
    },
    showUnit: {
      type: Boolean,
      default: true
    },
    maxlength: {
      type: Number,
      default: 0
    },
    formData: Object
  },
  emits: ["openPicker", "update:modelValue"],
  setup(__props, { emit: __emit }) {
    const props = __props;
    const instance = Vue.getCurrentInstance();
    const ctx = instance.proxy;
    const realMaxLength = Vue.computed(() => {
      return Math.floor(props.maxLength / (ctx.$dbFieldLengthRatio || 1)) || "-";
    });
    const unit = Vue.ref("");
    const fieldInputRef = Vue.ref();
    const [recordCursor, setCursor] = useCursor.useCursor();
    const emit = __emit;
    const textValue = Vue.ref("");
    const compValue = Vue.computed({
      get: () => props.modelValue,
      set: (val) => {
        emit("update:modelValue", val);
      }
    });
    Vue.watch(
      () => props.modelValue,
      (newVal) => {
        unit.value = formatterUtil.getUnit(props.dataFormatter);
        if (!props.fieldType || props.fieldType === "text") {
          textValue.value = formatterUtil.textFormatter(newVal, props.dataFormatter);
        } else {
          textValue.value = newVal;
        }
      },
      { immediate: true, flush: "post" }
    );
    const compClickable = Vue.computed(() => {
      if (props.disabled || props.readonly) {
        return false;
      }
      return true;
    });
    var isComposing = false;
    const handleInput = (e) => {
      if (isComposing) return;
      const inputElement = e.target;
      recordCursor(inputElement);
      inputElement.value = formatterUtil.textFormatter4Input(
        inputElement.value,
        props.dataFormatter
      );
      setCursor(inputElement);
      const unformattedValue = formatterUtil.reverseTextFormatter(
        inputElement.value,
        props.dataFormatter
      );
      compValue.value = unformattedValue;
    };
    const handleCompositionStart = () => {
      isComposing = true;
    };
    const handleCompositionEnd = (e) => {
      isComposing = false;
      doSomethingFunction(e);
    };
    const doSomethingFunction = (e) => {
      const inputElement = e.target;
      recordCursor(inputElement);
      inputElement.value = formatterUtil.textFormatter4Input(
        inputElement.value,
        props.dataFormatter
      );
      setCursor(inputElement);
      const unformattedValue = formatterUtil.reverseTextFormatter(
        inputElement.value,
        props.dataFormatter
      );
      compValue.value = unformattedValue;
    };
    return (_ctx, _cache) => {
      const _component_van_field = index.Field;
      return Vue.openBlock(), Vue.createElementBlock(Vue.Fragment, null, [
        Vue.createVNode(_component_van_field, Vue.mergeProps(_ctx.$attrs, {
          type: _ctx.$attrs.type == "number" ? "text" : _ctx.$attrs.type,
          modelValue: Vue.unref(textValue),
          "onUpdate:modelValue": _cache[0] || (_cache[0] = ($event) => Vue.isRef(textValue) ? textValue.value = $event : null),
          maxlength: Vue.unref(realMaxLength),
          disabled: __props.disabled,
          readonly: __props.readonly,
          clearable: "",
          clickable: Vue.unref(compClickable),
          "clear-trigger": "focus",
          onClick: _cache[1] || (_cache[1] = ($event) => !__props.disabled && emit("openPicker")),
          ref_key: "fieldInputRef",
          ref: fieldInputRef,
          onInput: handleInput,
          onCompositionstart: handleCompositionStart,
          onCompositionend: handleCompositionEnd,
          "input-align": "right"
        }), Vue.createSlots({ _: 2 }, [
          Vue.renderList(_ctx.$slots, (item, key) => {
            return {
              name: key,
              fn: Vue.withCtx(() => [
                Vue.renderSlot(_ctx.$slots, key)
              ])
            };
          }),
          __props.disabled ? {
            name: "input",
            fn: Vue.withCtx(() => [
              Vue.createElementVNode("div", null, Vue.toDisplayString(Vue.unref(textValue)), 1)
            ]),
            key: "0"
          } : void 0,
          __props.showUnit && Vue.unref(unit) ? {
            name: "button",
            fn: Vue.withCtx(() => [
              Vue.createTextVNode(Vue.toDisplayString(Vue.unref(unit)), 1)
            ]),
            key: "1"
          } : void 0
        ]), 1040, ["type", "modelValue", "maxlength", "disabled", "readonly", "clickable"]),
        Vue.withDirectives(Vue.createElementVNode("input", {
          type: "hidden",
          "onUpdate:modelValue": _cache[2] || (_cache[2] = ($event) => Vue.isRef(compValue) ? compValue.value = $event : null)
        }, null, 512), [
          [Vue.vModelText, Vue.unref(compValue)]
        ]),
        Vue.renderSlot(_ctx.$slots, "default")
      ], 64);
    };
  }
};
exports.default = _sfc_main;
