function wavesDirective(app) {
  app.directive("waves", {
    mounted(el, binding) {
      el.classList.add("waves-effect");
      binding.value && el.classList.add(`waves-${binding.value}`);
      function setConvertStyle(obj) {
        let style = "";
        for (let i in obj) {
          if (obj.hasOwnProperty(i)) style += `${i}:${obj[i]};`;
        }
        return style;
      }
      function onCurrentClick(e) {
        let elDiv = document.createElement("div");
        elDiv.classList.add("waves-ripple");
        el.appendChild(elDiv);
        let styles = {
          left: `${e.layerX}px`,
          top: `${e.layerY}px`,
          opacity: 1,
          transform: `scale(${el.clientWidth / 100 * 10})`,
          "transition-duration": `750ms`,
          "transition-timing-function": `cubic-bezier(0.250, 0.460, 0.450, 0.940)`
        };
        elDiv.setAttribute("style", setConvertStyle(styles));
        setTimeout(() => {
          elDiv.setAttribute(
            "style",
            setConvertStyle({
              opacity: 0,
              transform: styles.transform,
              left: styles.left,
              top: styles.top
            })
          );
          setTimeout(() => {
            elDiv && el.removeChild(elDiv);
          }, 750);
        }, 450);
      }
      el.addEventListener("mousedown", onCurrentClick, false);
    },
    unmounted(el) {
      el.addEventListener("mousedown", () => {
      });
    }
  });
}
function dragDirective(app) {
  app.directive("drag", {
    mounted(el, binding) {
      if (!binding.value) return false;
      const dragDom = document.querySelector(binding.value[0]);
      const dragHeader = document.querySelector(binding.value[1]);
      dragHeader.onmouseover = () => dragHeader.style.cursor = `move`;
      function down(e, type) {
        const disX = type === "pc" ? e.clientX - dragHeader.offsetLeft : e.touches[0].clientX - dragHeader.offsetLeft;
        const disY = type === "pc" ? e.clientY - dragHeader.offsetTop : e.touches[0].clientY - dragHeader.offsetTop;
        const screenWidth = document.body.clientWidth;
        const screenHeight = document.documentElement.clientHeight;
        const dragDomWidth = dragDom.offsetWidth;
        const dragDomheight = dragDom.offsetHeight;
        const minDragDomLeft = dragDom.offsetLeft;
        const maxDragDomLeft = screenWidth - dragDom.offsetLeft - dragDomWidth;
        const minDragDomTop = dragDom.offsetTop;
        const maxDragDomTop = screenHeight - dragDom.offsetTop - dragDomheight;
        let styL = getComputedStyle(dragDom).left;
        let styT = getComputedStyle(dragDom).top;
        if (styL.includes("%")) {
          styL = +document.body.clientWidth * (+styL.replace(/\%/g, "") / 100);
          styT = +document.body.clientHeight * (+styT.replace(/\%/g, "") / 100);
        } else {
          styL = +styL.replace(/\px/g, "");
          styT = +styT.replace(/\px/g, "");
        }
        return {
          disX,
          disY,
          minDragDomLeft,
          maxDragDomLeft,
          minDragDomTop,
          maxDragDomTop,
          styL,
          styT
        };
      }
      function move(e, type, obj) {
        let { disX, disY, minDragDomLeft, maxDragDomLeft, minDragDomTop, maxDragDomTop, styL, styT } = obj;
        let left = type === "pc" ? e.clientX - disX : e.touches[0].clientX - disX;
        let top = type === "pc" ? e.clientY - disY : e.touches[0].clientY - disY;
        if (-left > minDragDomLeft) {
          left = -minDragDomLeft;
        } else if (left > maxDragDomLeft) {
          left = maxDragDomLeft;
        }
        if (-top > minDragDomTop) {
          top = -minDragDomTop;
        } else if (top > maxDragDomTop) {
          top = maxDragDomTop;
        }
        dragDom.style.cssText += `;left:${left + styL}px;top:${top + styT}px;`;
      }
      dragHeader.onmousedown = (e) => {
        const obj = down(e, "pc");
        document.onmousemove = (e2) => {
          move(e2, "pc", obj);
        };
        document.onmouseup = () => {
          document.onmousemove = null;
          document.onmouseup = null;
        };
      };
      dragHeader.ontouchstart = (e) => {
        const obj = down(e, "app");
        document.ontouchmove = (e2) => {
          move(e2, "app", obj);
        };
        document.ontouchend = () => {
          document.ontouchmove = null;
          document.ontouchend = null;
        };
      };
    }
  });
}
function reclickDirective(app) {
  let opacity = "";
  app.directive("reclick", {
    mounted(el, time) {
      el.addEventListener("click", () => {
        if (!el.disabled) {
          el.disabled = true;
          opacity = el.style.opacity;
          el.style.opacity = "0.2";
          setTimeout(
            () => {
              el.disabled = false;
              el.style.opacity = opacity;
            },
            time.value === void 0 ? 800 : time.value
          );
        }
      });
    },
    unmounted(el) {
      el.disabled = false;
      el.style.opacity = opacity;
    }
  });
}

export { dragDirective, reclickDirective, wavesDirective };
