"use strict";

exports.__esModule = true;
exports.default = void 0;

var _dom = _interopRequireDefault(require("../../shared/dom7"));

var _utils = require("../../shared/utils");

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

var TouchRipple = /*#__PURE__*/function () {
  function TouchRipple($el, x, y) {
    var ripple = this;
    if (!$el) return undefined;
    var box = $el[0].getBoundingClientRect();
    var center = {
      x: x - box.left,
      y: y - box.top
    };
    var width = box.width;
    var height = box.height;
    var diameter = Math.max(Math.pow(Math.pow(height, 2) + Math.pow(width, 2), 0.5), 48);
    ripple.$rippleWaveEl = (0, _dom.default)("<div class=\"ripple-wave\" style=\"width: " + diameter + "px; height: " + diameter + "px; margin-top:-" + diameter / 2 + "px; margin-left:-" + diameter / 2 + "px; left:" + center.x + "px; top:" + center.y + "px;\"></div>");
    $el.prepend(ripple.$rippleWaveEl);
    ripple.rippleTransform = "translate3d(" + (-center.x + width / 2) + "px, " + (-center.y + height / 2) + "px, 0) scale(1)";
    (0, _utils.nextFrame)(function () {
      if (!ripple || !ripple.$rippleWaveEl) return;
      ripple.$rippleWaveEl.transform(ripple.rippleTransform);
    });
    return ripple;
  }

  var _proto = TouchRipple.prototype;

  _proto.destroy = function destroy() {
    var ripple = this;

    if (ripple.$rippleWaveEl) {
      ripple.$rippleWaveEl.remove();
    }

    Object.keys(ripple).forEach(function (key) {
      ripple[key] = null;
      delete ripple[key];
    });
    ripple = null;
  };

  _proto.remove = function remove() {
    var ripple = this;
    if (ripple.removing) return;
    var $rippleWaveEl = this.$rippleWaveEl;
    var rippleTransform = this.rippleTransform;
    var removeTimeout = (0, _utils.nextTick)(function () {
      ripple.destroy();
    }, 400);
    ripple.removing = true;
    $rippleWaveEl.addClass('ripple-wave-fill').transform(rippleTransform.replace('scale(1)', 'scale(1.01)')).transitionEnd(function () {
      clearTimeout(removeTimeout);
      (0, _utils.nextFrame)(function () {
        $rippleWaveEl.addClass('ripple-wave-out').transform(rippleTransform.replace('scale(1)', 'scale(1.01)'));
        removeTimeout = (0, _utils.nextTick)(function () {
          ripple.destroy();
        }, 700);
        $rippleWaveEl.transitionEnd(function () {
          clearTimeout(removeTimeout);
          ripple.destroy();
        });
      });
    });
  };

  return TouchRipple;
}();

exports.default = TouchRipple;