'use strict';

var isVisible = require('../../vc-util/Dom/isVisible.js');
var KeyCode = require('../../_util/KeyCode.js');
var raf = require('../../_util/raf.js');

const scrollIds = /* @__PURE__ */ new Map();
function waitElementReady(element, callback) {
  let id;
  function tryOrNextFrame() {
    if (isVisible.default(element)) {
      callback();
    } else {
      id = raf.default(() => {
        tryOrNextFrame();
      });
    }
  }
  tryOrNextFrame();
  return () => {
    raf.default.cancel(id);
  };
}
function scrollTo(element, to, duration) {
  if (scrollIds.get(element))
    raf.default.cancel(scrollIds.get(element));
  if (duration <= 0) {
    scrollIds.set(
      element,
      raf.default(() => {
        element.scrollTop = to;
      })
    );
    return;
  }
  const difference = to - element.scrollTop;
  const perTick = difference / duration * 10;
  scrollIds.set(
    element,
    raf.default(() => {
      element.scrollTop += perTick;
      if (element.scrollTop !== to)
        scrollTo(element, to, duration - 10);
    })
  );
}
function createKeydownHandler(event, { onLeftRight, onCtrlLeftRight, onUpDown, onPageUpDown, onEnter }) {
  const { which, ctrlKey, metaKey } = event;
  switch (which) {
    case KeyCode.default.LEFT:
      if (ctrlKey || metaKey) {
        if (onCtrlLeftRight) {
          onCtrlLeftRight(-1);
          return true;
        }
      } else if (onLeftRight) {
        onLeftRight(-1);
        return true;
      }
      break;
    case KeyCode.default.RIGHT:
      if (ctrlKey || metaKey) {
        if (onCtrlLeftRight) {
          onCtrlLeftRight(1);
          return true;
        }
      } else if (onLeftRight) {
        onLeftRight(1);
        return true;
      }
      break;
    case KeyCode.default.UP:
      if (onUpDown) {
        onUpDown(-1);
        return true;
      }
      break;
    case KeyCode.default.DOWN:
      if (onUpDown) {
        onUpDown(1);
        return true;
      }
      break;
    case KeyCode.default.PAGE_UP:
      if (onPageUpDown) {
        onPageUpDown(-1);
        return true;
      }
      break;
    case KeyCode.default.PAGE_DOWN:
      if (onPageUpDown) {
        onPageUpDown(1);
        return true;
      }
      break;
    case KeyCode.default.ENTER:
      if (onEnter) {
        onEnter();
        return true;
      }
      break;
  }
  return false;
}
function getDefaultFormat(format, picker, showTime, use12Hours) {
  let mergedFormat = format;
  if (!mergedFormat) {
    switch (picker) {
      case "time":
        mergedFormat = use12Hours ? "hh:mm:ss a" : "HH:mm:ss";
        break;
      case "week":
        mergedFormat = "gggg-wo";
        break;
      case "month":
        mergedFormat = "YYYY-MM";
        break;
      case "quarter":
        mergedFormat = "YYYY-[Q]Q";
        break;
      case "year":
        mergedFormat = "YYYY";
        break;
      default:
        mergedFormat = showTime ? "YYYY-MM-DD HH:mm:ss" : "YYYY-MM-DD";
    }
  }
  return mergedFormat;
}
function getInputSize(picker, format, generateConfig) {
  const defaultSize = picker === "time" ? 8 : 10;
  const length = typeof format === "function" ? format(generateConfig.getNow()).length : format.length;
  return Math.max(defaultSize, length) + 2;
}
let globalClickFunc = null;
const clickCallbacks = /* @__PURE__ */ new Set();
function addGlobalMousedownEvent(callback) {
  if (!globalClickFunc && typeof window !== "undefined" && window.addEventListener) {
    globalClickFunc = (e) => {
      [...clickCallbacks].forEach((queueFunc) => {
        queueFunc(e);
      });
    };
    window.addEventListener("mousedown", globalClickFunc);
  }
  clickCallbacks.add(callback);
  return () => {
    clickCallbacks.delete(callback);
    if (clickCallbacks.size === 0) {
      window.removeEventListener("mousedown", globalClickFunc);
      globalClickFunc = null;
    }
  };
}
function getTargetFromEvent(e) {
  var _a;
  const target = e.target;
  if (e.composed && target.shadowRoot)
    return ((_a = e.composedPath) == null ? void 0 : _a.call(e)[0]) || target;
  return target;
}
const getYearNextMode = (next) => {
  if (next === "month" || next === "date")
    return "year";
  return next;
};
const getMonthNextMode = (next) => {
  if (next === "date")
    return "month";
  return next;
};
const getQuarterNextMode = (next) => {
  if (next === "month" || next === "date")
    return "quarter";
  return next;
};
const getWeekNextMode = (next) => {
  if (next === "date")
    return "week";
  return next;
};
const PickerModeMap = {
  year: getYearNextMode,
  month: getMonthNextMode,
  quarter: getQuarterNextMode,
  week: getWeekNextMode,
  time: null,
  date: null
};
function elementsContains(elements, target) {
  if (process.env.NODE_ENV === "test")
    return false;
  return elements.some((ele) => ele && ele.contains(target));
}

exports.PickerModeMap = PickerModeMap;
exports.addGlobalMousedownEvent = addGlobalMousedownEvent;
exports.createKeydownHandler = createKeydownHandler;
exports.elementsContains = elementsContains;
exports.getDefaultFormat = getDefaultFormat;
exports.getInputSize = getInputSize;
exports.getTargetFromEvent = getTargetFromEvent;
exports.scrollTo = scrollTo;
exports.waitElementReady = waitElementReady;
//# sourceMappingURL=uiUtil.js.map
