/**
 * ldesign v0.2.14
 * (c) 2022/11/2 16:16 built by swimly
 * @license MIT
 */

import Vue from 'vue';
import raf from 'raf';
import isString from 'lodash/isString';
import { l as log } from '../_chunks/dep-70c789e5.js';
import { easeInOutCubic } from './easing.js';
import '../_chunks/dep-5210251b.js';

const isServer = Vue.prototype.$isServer || typeof window === "undefined";
const trim = str => (str || "").replace(/^[\s\uFEFF]+|[\s\uFEFF]+$/g, "");
const off = (() => {
  if (!isServer && document.removeEventListener) {
    return (element, event, handler, options) => {
      if (element && event) element.removeEventListener(event, handler, options);
    };
  }
  return (element, event, handler) => {
    if (element && event) element.detachEvent(`on${event}`, handler);
  };
})();
const on = (() => {
  if (!isServer && document.addEventListener) {
    return (element, event, handler, options) => {
      if (element && event && handler) {
        element.addEventListener(event, handler, options);
        return () => off(element, event, handler, options);
      }
    };
  }
  return (element, event, handler) => {
    if (element && event && handler) {
      element.attachEvent(`on${event}`, handler);
      return () => off(element, event, handler);
    }
  };
})();
function once(element, event, handler, options) {
  const handlerFn = typeof handler === "function" ? handler : handler.handleEvent;
  const callback = evt => {
    handlerFn(evt);
    off(element, event, callback, options);
  };
  on(element, event, callback, options);
}
function hasClass(el, cls) {
  if (!el || !cls) return false;
  if (cls.includes(" ")) log.warn("utils/dom.ts", "className should not contain space.");
  if (el.classList) return el.classList.contains(cls);
  return ` ${el.className} `.includes(` ${cls} `);
}
function addClass(el, cls) {
  if (!el) return;
  let curClass = el.className;
  const classes = (cls || "").split(" ");
  for (let i = 0, j = classes.length; i < j; i++) {
    const clsName = classes[i];
    if (!clsName) continue;
    if (el.classList) el.classList.add(clsName);else if (!hasClass(el, clsName)) curClass += ` ${clsName}`;
  }
  if (!el.classList) el.className = curClass;
}
function removeClass(el, cls) {
  if (!el || !cls) return;
  const classes = cls.split(" ");
  let curClass = ` ${el.className} `;
  for (let i = 0, j = classes.length; i < j; i++) {
    const clsName = classes[i];
    if (!clsName) continue;
    if (el.classList) el.classList.remove(clsName);else if (hasClass(el, clsName)) curClass = curClass.replace(` ${clsName} `, " ");
  }
  if (!el.classList) el.className = trim(curClass);
}
const getAttach = (node, triggerNode) => {
  const attachNode = typeof node === "function" ? node(triggerNode) : node;
  if (!attachNode) return document.body;
  if (isString(attachNode)) return document.querySelector(attachNode);
  if (attachNode instanceof HTMLElement) return attachNode;
  return document.body;
};
const getScrollContainer = function () {
  let container = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : "body";
  if (isString(container)) return document.querySelector(container);
  if (typeof container === "function") return container();
  return container;
};
function isWindow(obj) {
  return obj && obj === obj.window;
}
function getScroll(target, isLeft) {
  if (typeof window === "undefined" || !target) return 0;
  const method = isLeft ? "scrollLeft" : "scrollTop";
  let result = 0;
  if (isWindow(target)) result = target[isLeft ? "pageXOffset" : "pageYOffset"];else if (target instanceof Document) result = target.documentElement[method];else if (target) result = target[method];
  return result;
}
function scrollTo(target, opt) {
  const {
    container = window,
    duration = 450,
    easing = easeInOutCubic
  } = opt;
  const scrollTop = getScroll(container);
  const startTime = Date.now();
  return new Promise(res => {
    const fnc = () => {
      const timestamp = Date.now();
      const time = timestamp - startTime;
      const nextScrollTop = easing(Math.min(time, duration), scrollTop, target, duration);
      if (isWindow(container)) container.scrollTo(window.pageXOffset, nextScrollTop);else if (container instanceof HTMLDocument || container.constructor.name === "HTMLDocument") container.documentElement.scrollTop = nextScrollTop;else container.scrollTop = nextScrollTop;
      if (time < duration) {
        raf(fnc);
      } else {
        raf(res);
      }
    };
    raf(fnc);
  });
}
function containerDom(parent, child) {
  if (parent && child) {
    let pNode = child;
    while (pNode) {
      if (parent === pNode) return true;
      const {
        parentNode
      } = pNode;
      pNode = parentNode;
    }
  }
  return false;
}
const clickOut = (els, cb) => {
  on(document, "click", event => {
    if (Array.isArray(els)) {
      const isFlag = Array.from(els).every(item => containerDom(item, event.target) === false);
      isFlag && cb && cb();
    } else {
      if (containerDom(els, event.target)) return false;
      cb && cb();
    }
  });
};
const isNodeOverflow = ele => {
  const {
    clientWidth = 0,
    scrollWidth = 0
  } = ele;
  return scrollWidth > clientWidth;
};
const removeDom = el => {
  if (el.remove) {
    el.remove();
  } else {
    el.parentNode.removeChild(el);
  }
};

export { addClass, clickOut, getAttach, getScroll, getScrollContainer, hasClass, isNodeOverflow, isServer, off, on, once, removeClass, removeDom, scrollTo, trim };
//# sourceMappingURL=dom.js.map
