/**
 * ldesign v0.3.13
 * (c) 2023 ldesign
 * @license MIT
 */

import { ref, onMounted, onUnmounted } from 'vue';
import setStyle from '../utils/set-style.mjs';
import useKeepAnimation from './useKeepAnimation.mjs';
import { usePrefixClass } from './useConfig.mjs';
import '../_chunks/dep-40d69a9c.mjs';
import '../_chunks/dep-eba0ea6e.mjs';
import '../_chunks/dep-9cf86cf1.mjs';
import '../_chunks/dep-bc1d737a.mjs';
import '../_chunks/dep-4d7a675b.mjs';
import '../_chunks/dep-5717e263.mjs';
import '../_chunks/dep-469b4ae0.mjs';
import '../_chunks/dep-255303ba.mjs';
import '../_chunks/dep-a8bcacea.mjs';
import '../_chunks/dep-2e7f59b1.mjs';
import '../_chunks/dep-855db8bc.mjs';
import '../_chunks/dep-a87f3e4c.mjs';
import '../_chunks/dep-1e566bb5.mjs';
import '../_chunks/dep-1434aa15.mjs';
import '../_chunks/dep-3da99438.mjs';
import '../_chunks/dep-b57180d0.mjs';
import '../_chunks/dep-c8d7b6b4.mjs';
import '../_chunks/dep-e5eb9f23.mjs';
import '../_chunks/dep-9fe2ef12.mjs';
import '../_chunks/dep-672e7d1a.mjs';
import '../_chunks/dep-42a19209.mjs';
import '../_chunks/dep-9262ee2f.mjs';
import '../_chunks/dep-dcc83161.mjs';
import '../_chunks/dep-0fc02184.mjs';
import '../_chunks/dep-f1994936.mjs';
import '../_chunks/dep-345e1be3.mjs';
import '../_common/js/global-config/default-config.mjs';
import '../_common/js/global-config/locale/zh_CN.mjs';
import '../_chunks/dep-9888611c.mjs';
import '../_chunks/dep-697a1365.mjs';
import '../config-provider/useConfig.mjs';
import '../_chunks/dep-095107f7.mjs';
import '../_chunks/dep-c989dff3.mjs';
import '../_chunks/dep-334a4f6b.mjs';
import '../_chunks/dep-8d3e9aa3.mjs';
import '../_chunks/dep-876fb3ec.mjs';
import '../config-provider/type.mjs';

var period = 200;
var noneRippleBg = "rgba(0, 0, 0, 0)";
var defaultRippleColor = "rgba(0, 0, 0, 0.35)";
var getRippleColor = function getRippleColor(el, fixedRippleColor) {
  var _el$dataset;
  if (fixedRippleColor) return fixedRippleColor;
  if (el !== null && el !== void 0 && (_el$dataset = el.dataset) !== null && _el$dataset !== void 0 && _el$dataset.ripple) {
    var rippleColor = el.dataset.ripple;
    return rippleColor;
  }
  var cssVariable = getComputedStyle(el).getPropertyValue("--ripple-color");
  if (cssVariable) return cssVariable;
  return defaultRippleColor;
};
function useRipple(el, fixedRippleColor) {
  var rippleContainer = ref(null);
  var classPrefix = usePrefixClass();
  var _useKeepAnimation = useKeepAnimation(),
    keepRipple = _useKeepAnimation.keepRipple;
  var handleAddRipple = function handleAddRipple(e) {
    var dom = el.value;
    var rippleColor = getRippleColor(dom, fixedRippleColor === null || fixedRippleColor === void 0 ? void 0 : fixedRippleColor.value);
    if (e.button !== 0 || !el || !keepRipple) return;
    if (dom.classList.contains("".concat(classPrefix.value, "-is-active")) || dom.classList.contains("".concat(classPrefix.value, "-is-disabled")) || dom.classList.contains("".concat(classPrefix.value, "-is-checked")) || dom.classList.contains("".concat(classPrefix.value, "-is-loading"))) return;
    var elStyle = getComputedStyle(dom);
    var elBorder = parseInt(elStyle.borderWidth, 10);
    var border = elBorder > 0 ? elBorder : 0;
    var width = dom.offsetWidth;
    var height = dom.offsetHeight;
    if (rippleContainer.value.parentNode === null) {
      setStyle(rippleContainer.value, {
        position: "absolute",
        left: "".concat(0 - border, "px"),
        top: "".concat(0 - border, "px"),
        width: "".concat(width, "px"),
        height: "".concat(height, "px"),
        borderRadius: elStyle.borderRadius,
        pointerEvents: "none",
        overflow: "hidden"
      });
      dom.appendChild(rippleContainer.value);
    }
    var ripple = document.createElement("div");
    setStyle(ripple, {
      marginTop: "0",
      marginLeft: "0",
      right: "".concat(width, "px"),
      width: "".concat(width + 20, "px"),
      height: "100%",
      transition: "transform ".concat(period, "ms cubic-bezier(.38, 0, .24, 1), background ").concat(period * 2, "ms linear"),
      transform: "skewX(-8deg)",
      pointerEvents: "none",
      position: "absolute",
      zIndex: 0,
      backgroundColor: rippleColor,
      opacity: "0.9"
    });
    var elMap = /* @__PURE__ */new WeakMap();
    for (var n = dom.children.length, i = 0; i < n; ++i) {
      var child = dom.children[i];
      if (child.style.zIndex === "" && child !== rippleContainer.value) {
        child.style.zIndex = "1";
        elMap.set(child, true);
      }
    }
    var initPosition = dom.style.position ? dom.style.position : getComputedStyle(dom).position;
    if (initPosition === "" || initPosition === "static") dom.style.position = "relative";
    rippleContainer.value.insertBefore(ripple, rippleContainer.value.firstChild);
    setTimeout(function () {
      ripple.style.transform = "translateX(".concat(width, "px)");
    }, 0);
    var handleClearRipple = function handleClearRipple() {
      ripple.style.backgroundColor = noneRippleBg;
      if (!el.value) return;
      el.value.removeEventListener("pointerup", handleClearRipple, false);
      el.value.removeEventListener("pointerleave", handleClearRipple, false);
      setTimeout(function () {
        ripple.remove();
        if (rippleContainer.value.children.length === 0) rippleContainer.value.remove();
      }, period * 2 + 100);
    };
    el.value.addEventListener("pointerup", handleClearRipple, false);
    el.value.addEventListener("pointerleave", handleClearRipple, false);
  };
  onMounted(function () {
    var dom = el === null || el === void 0 ? void 0 : el.value;
    if (!dom) return;
    rippleContainer.value = document.createElement("div");
    dom.addEventListener("pointerdown", handleAddRipple, false);
  });
  onUnmounted(function () {
    var _el$value;
    el === null || el === void 0 ? void 0 : (_el$value = el.value) === null || _el$value === void 0 ? void 0 : _el$value.removeEventListener("pointerdown", handleAddRipple, false);
  });
}

export { useRipple as default };
//# sourceMappingURL=useRipple.mjs.map
