/*
 * @Author: codingfly
 * @Description:
 * @Date: 2021-11-17 17:03:27
 * @LastEditTime: 2021-11-18 09:38:34
 * @FilePath: \ui_model\src\create-selector-query\index.web.ts
 */
// eslint-disable-next-line @typescript-eslint/no-empty-function
var HTML_NODES = ['[object HTMLCollection]', '[object NodeList]'];

var createSelectorQuery = function createSelectorQuery() {
  var query = {};
  var eles = [];

  query.select = function (selector) {
    var ele = null;
    ele = document.querySelector(selector);
    eles.push(ele);
    return this;
  };

  query.selectAll = function (selector) {
    var ele = [];
    ele = document.querySelectorAll(selector);
    eles.push(ele);
    return this;
  };

  query.boundingClientRect = function () {
    eles = eles.map(function (item) {
      var newItem = item;

      if (HTML_NODES.includes(Object.prototype.toString.call(item))) {
        var htmlArr = [];

        for (var index = 0; index < item.length; index++) {
          htmlArr.push(item[index]);
        }

        newItem = htmlArr.map(function (i) {
          var rect = i.getBoundingClientRect();
          return {
            bottom: rect.bottom,
            height: rect.height,
            // 由于 getBoundingClientRect 方法获取的是相对视窗的，所以这里改写成 i.offsetLeft 来使其相对的元素为最近的position不为static的祖先元素。
            left: i.offsetLeft,
            right: rect.right,
            top: rect.top,
            width: i.offsetWidth,
            x: rect.x,
            y: rect.y
          };
        });
      } else {
        var rect = newItem.getBoundingClientRect();
        newItem = {
          bottom: rect.bottom,
          height: rect.height,
          left: newItem.offsetLeft,
          right: rect.right,
          top: rect.top,
          width: newItem.offsetWidth,
          x: rect.x,
          y: rect.y
        };
      }

      return newItem;
    });
    return this;
  };

  query.exec = function (callback) {
    callback === null || callback === void 0 ? void 0 : callback(eles);
  };

  return query;
};

export default createSelectorQuery;