'use strict';

var setStyle = require('../dom/setStyle.cjs');
var getScrollBarSize = require('./getScrollBarSize.cjs');

var __defProp = Object.defineProperty;
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
var __publicField = (obj, key, value) => {
  __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
  return value;
};
let locks = [];
const scrollingEffectClassName = "ant-scrolling-effect";
const scrollingEffectClassNameReg = new RegExp(`${scrollingEffectClassName}`, "g");
let uuid = 0;
const cacheStyle = /* @__PURE__ */ new Map();
class ScrollLocker {
  constructor(options) {
    __publicField(this, "lockTarget");
    __publicField(this, "options");
    __publicField(this, "getContainer", () => {
      var _a;
      return (_a = this.options) == null ? void 0 : _a.container;
    });
    // if options change...
    __publicField(this, "reLock", (options) => {
      const findLock = locks.find(({ target }) => target === this.lockTarget);
      if (findLock)
        this.unLock();
      this.options = options;
      if (findLock) {
        findLock.options = options;
        this.lock();
      }
    });
    __publicField(this, "lock", () => {
      var _a;
      if (locks.some(({ target }) => target === this.lockTarget))
        return;
      if (locks.some(({ options }) => {
        var _a2;
        return (options == null ? void 0 : options.container) === ((_a2 = this.options) == null ? void 0 : _a2.container);
      })) {
        locks = [...locks, { target: this.lockTarget, options: this.options }];
        return;
      }
      let scrollBarSize = 0;
      const container = ((_a = this.options) == null ? void 0 : _a.container) || document.body;
      if (container === document.body && window.innerWidth - document.documentElement.clientWidth > 0 || container.scrollHeight > container.clientHeight)
        scrollBarSize = getScrollBarSize.getScrollBarSize();
      const containerClassName = container.className;
      if (locks.filter(({ options }) => {
        var _a2;
        return (options == null ? void 0 : options.container) === ((_a2 = this.options) == null ? void 0 : _a2.container);
      }).length === 0) {
        cacheStyle.set(
          container,
          setStyle.setStyle(
            {
              width: scrollBarSize !== 0 ? `calc(100% - ${scrollBarSize}px)` : void 0,
              overflow: "hidden",
              overflowX: "hidden",
              overflowY: "hidden"
            },
            {
              element: container
            }
          )
        );
      }
      if (!scrollingEffectClassNameReg.test(containerClassName)) {
        const addClassName = `${containerClassName} ${scrollingEffectClassName}`;
        container.className = addClassName.trim();
      }
      locks = [...locks, { target: this.lockTarget, options: this.options }];
    });
    __publicField(this, "unLock", () => {
      var _a;
      const findLock = locks.find(({ target }) => target === this.lockTarget);
      locks = locks.filter(({ target }) => target !== this.lockTarget);
      if (!findLock || locks.some(({ options }) => {
        var _a2;
        return (options == null ? void 0 : options.container) === ((_a2 = findLock.options) == null ? void 0 : _a2.container);
      }))
        return;
      const container = ((_a = this.options) == null ? void 0 : _a.container) || document.body;
      const containerClassName = container.className;
      if (!scrollingEffectClassNameReg.test(containerClassName))
        return;
      setStyle.setStyle(cacheStyle.get(container), { element: container });
      cacheStyle.delete(container);
      container.className = container.className.replace(scrollingEffectClassNameReg, "").trim();
    });
    this.lockTarget = uuid++;
    this.options = options;
  }
}

exports.ScrollLocker = ScrollLocker;
//# sourceMappingURL=scrollLocker.cjs.map
