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

'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var _common_js_utils_setStyle = require('../_common/js/utils/set-style.js');
var config = require('../config.js');

var Ripple = {
  startTimeId: null,
  finishTimeId: null,
  inserted: function inserted(el, binding) {
    if (binding.value === false) return;
    var period = 200;
    var defaultBg = "rgba(0, 0, 0, 0.35)";
    var bg = binding.value === true || binding.value == null ? defaultBg : binding.value;
    var rippleContainer = document.createElement("div");
    var hasCreateContainer = false;
    var count = 0;
    el.addEventListener("pointerdown", function (e) {
      if (el.classList.contains("".concat(config.prefix, "-is-active")) || el.classList.contains("".concat(config.prefix, "-is-disabled")) || el.classList.contains("".concat(config.prefix, "-is-checked")) || el.classList.contains("".concat(config.prefix, "-is-loading"))) return;
      if (e.button !== 0) return;
      if (bg === defaultBg && el !== null && el !== void 0 && el.dataset.ripple) bg = el.dataset.ripple;
      var cssVariable = getComputedStyle(el).getPropertyValue("--ripple-color");
      if (cssVariable) bg = cssVariable;
      var elBorder = parseInt(getComputedStyle(el).borderWidth.replace("px", ""), 10);
      var border = elBorder > 0 ? elBorder : 0;
      var width = el.offsetWidth;
      var height = el.offsetHeight;
      var style = getComputedStyle(el);
      if (!hasCreateContainer) {
        hasCreateContainer = true;
        _common_js_utils_setStyle["default"](rippleContainer, {
          position: "absolute",
          left: "".concat(0 - border, "px"),
          top: "".concat(0 - border, "px"),
          width: "".concat(width, "px"),
          height: "".concat(height, "px"),
          borderRadius: style.borderRadius,
          pointerEvents: "none",
          overflow: "hidden"
        });
        el.appendChild(rippleContainer);
      }
      var ripple = document.createElement("div");
      _common_js_utils_setStyle["default"](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: bg,
        opacity: "0.9"
      });
      var elMap = /* @__PURE__ */new WeakMap();
      for (var n = el.children.length, i = 0; i < n; ++i) {
        var child = el.children[i];
        if (child.style.zIndex === "" && child !== rippleContainer) {
          child.style.zIndex = "1";
          elMap.set(child, true);
        }
      }
      var initPosition = el.style.position ? el.style.position : getComputedStyle(el).position;
      if (initPosition === "" || initPosition === "static") el.style.position = "relative";
      rippleContainer.insertBefore(ripple, rippleContainer.firstChild);
      count += 1;
      clearTimeout(Ripple.startTimeId);
      Ripple.startTimeId = setTimeout(function () {
        ripple.style.transform = "translateX(".concat(width, "px)");
      }, 0);
      var handleClearRipple = function handleClearRipple() {
        ripple.style.backgroundColor = "rgba(0, 0, 0, 0)";
        el.removeEventListener("pointerup", handleClearRipple, false);
        el.removeEventListener("pointerleave", handleClearRipple, false);
        setTimeout(function () {
          rippleContainer.removeChild(ripple);
          count -= 1;
          if (count > 0) return;
          el.style.position = initPosition !== "static" ? initPosition : "";
          for (var _n = el.children.length, _i = 0; _i < _n; ++_i) {
            var _child = el.children[_i];
            if (elMap.has(_child)) {
              _child.style.zIndex = "";
              elMap["delete"](_child);
            }
          }
          el.removeChild(rippleContainer);
          hasCreateContainer = false;
        }, period * 2 + 100);
      };
      el.addEventListener("pointerup", handleClearRipple, false);
      el.addEventListener("pointerleave", handleClearRipple, false);
    });
  }
};

exports["default"] = Ripple;
//# sourceMappingURL=ripple.js.map
