"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = scrollbarWidth;

var _canUseDom = _interopRequireDefault(require("can-use-dom"));

var _helpers = require("./helpers");

var _getLocale = require("../../../utils/getLocale");

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

var cachedScrollbarWidth = null;
var cachedDevicePixelRatio = null;

if (_canUseDom["default"]) {
  window.addEventListener("resize", function () {
    if (cachedDevicePixelRatio !== window.devicePixelRatio) {
      cachedDevicePixelRatio = window.devicePixelRatio;
      cachedScrollbarWidth = null;
    }
  });
}

function scrollbarWidth(el) {
  if (cachedScrollbarWidth === null) {
    var document = (0, _helpers.getElementDocument)(el);

    if (typeof document === "undefined") {
      cachedScrollbarWidth = 0;
      return cachedScrollbarWidth;
    }

    var body = document.body;
    var box = document.createElement("div");
    box.classList.add("simplebar-hide-scrollbar");
    body.appendChild(box);
    var width = box.getBoundingClientRect().right;
    body.removeChild(box);
    cachedScrollbarWidth = width;
  }

  return cachedScrollbarWidth;
}