export const vFloatFirective = function (app: any) {
  app.directive("inputFloat", {
    bind(el: any, binding: any, vnode: any) {
      const input = vnode.elm.children[0];
      input.addEventListener("compositionstart", () => {
        vnode.inputLocking = true;
      });
      input.addEventListener("compositionend", () => {
        vnode.inputLocking = false;
        input.dispatchEvent(new Event("input"));
      });
      input.addEventListener("input", () => {
        if (vnode.inputLocking) {
          return;
        }
        const oldValue = input.value;
        let newValue = input.value;
        newValue = newValue.replace(/[^\d.]/g, "");
        newValue = newValue.replace(/^\./g, "");
        newValue = newValue
          .replace(".", "$#$")
          .replace(/\./g, "")
          .replace("$#$", ".");
        //限制浮点数位数及整数位数
        // newValue = newValue.replace(/^(\-)*(\d+)\.(\d\d).*$/, "$1$2.$3");
        // if (newValue) {
        //   let arr = newValue.split(".");
        //   newValue = Number(arr[0]) + (arr[1] === undefined ? "" : "." + arr[1]); // 去掉开头多余的0
        // }
        // 判断是否需要更新，避免进入死循环
        if (newValue !== oldValue) {
          input.value = newValue;
          input.dispatchEvent(new Event("input")); // 通知v-model更新
        }
      });
      // input 事件无法处理小数点后面全是零的情况 因为无法确定用户输入的0是否真的应该清除，如3.02。放在blur中去处理
      input.addEventListener("blur", () => {
        const oldValue = input.value;
        let newValue = input.value;
        if (newValue) {
          newValue = Number(newValue).toString();
        }
        // 判断是否需要更新，避免进入死循环
        if (newValue !== oldValue) {
          input.value = newValue;
          input.dispatchEvent(new Event("input")); // 通知v-model更新
        }
      });
    },
  });
  //允许输入负数
  app.directive("inputFloatNegative", {
    bind(el: any, binding: any, vnode: any) {
      const input = vnode.elm.children[0];
      input.addEventListener("compositionstart", () => {
        vnode.inputLocking = true;
      });
      input.addEventListener("compositionend", () => {
        vnode.inputLocking = false;
        input.dispatchEvent(new Event("input"));
      });
      input.addEventListener("input", () => {
        if (vnode.inputLocking) {
          return;
        }
        const oldValue = input.value;
        let newValue = input.value;

        // if (!IsNegative) {
        //   //如果当前工艺不允许输入负值
        //   newValue = newValue.replace(/[^\d.]/g, "");
        //   newValue = newValue.replace(/^\./g, "");
        //   newValue = newValue
        //     .replace(".", "$#$")
        //     .replace(/\./g, "")
        //     .replace("$#$", ".");
        // } else {
        //如果当前工艺允许输入负值
        // eslint-disable-next-line
        newValue = newValue.replace(/[^\-^\.^\d]/i, "");
        newValue = newValue.replace(/^\./g, "");
        newValue = newValue
          .replace(".", "$#$")
          .replace("-", "$1#$1")
          .replace(/\./g, "")
          // eslint-disable-next-line
          .replace(/\-/g, "")
          .replace("$#$", ".")
          .replace("$1#$1", "-");
        // }
        //限制浮点数位数及整数位数
        // 判断是否需要更新，避免进入死循环

        if (newValue !== oldValue) {
          input.value = newValue;
          input.dispatchEvent(new Event("input")); // 通知v-model更新
        }
      });
      // input 事件无法处理小数点后面全是零的情况 因为无法确定用户输入的0是否真的应该清除，如3.02。放在blur中去处理
      input.addEventListener("blur", () => {
        const oldValue = input.value;
        let newValue = input.value;
        if (newValue) {
          newValue = Number(newValue).toString();
        }
        // 判断是否需要更新，避免进入死循环
        if (newValue !== oldValue) {
          input.value = newValue;
          input.dispatchEvent(new Event("input")); // 通知v-model更新
        }
      });
    },
  });
};
