export default function useLoadConsole() {
  let eggCounter = [];
  let eggShow = false;
  const $egg = (count, time) => {
    if (eggShow) return;
    const current = new Date().getTime();
    eggCounter.push(current);
    if (eggCounter.length >= count) {
      eggCounter.splice(0, eggCounter.length - count);
      if (current - eggCounter[0] <= time) {
        eggShow = true;
        loadScript(
          "https://cdn.bootcss.com/vConsole/3.14.6/vconsole.min.js",
          () => new window.VConsole()
        );
      }
    }
  };

  function loadScript(url, callback) {
    const script = document.createElement("script");
    script.type = "text/javascript";
    if (typeof callback !== "undefined") {
      if (script.readyState) {
        script.onreadystatechange = function () {
          if (
            script.readyState === "loaded" ||
            script.readyState === "complete"
          ) {
            script.onreadystatechange = null;
            callback();
          }
        };
      } else {
        script.onload = function () {
          callback();
        };
      }
    }
    script.src = url;
    document.body.appendChild(script);
  }

  return $egg;
}
