import { DEFAULT_DURATION } from '../message/message.js';

var __defProp = Object.defineProperty;
var __defProps = Object.defineProperties;
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __propIsEnum = Object.prototype.propertyIsEnumerable;
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
var __spreadValues = (a, b) => {
  for (var prop in b || (b = {}))
    if (__hasOwnProp.call(b, prop))
      __defNormalProp(a, prop, b[prop]);
  if (__getOwnPropSymbols)
    for (var prop of __getOwnPropSymbols(b)) {
      if (__propIsEnum.call(b, prop))
        __defNormalProp(a, prop, b[prop]);
    }
  return a;
};
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
var __publicField = (obj, key, value) => {
  __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
  return value;
};
function makeUUID(length = 25) {
  let result = "";
  const characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
  const charactersLength = characters.length;
  for (let i = 0; i < length; i++) {
    result += characters.charAt(Math.floor(Math.random() * charactersLength));
  }
  return result;
}
class Batcher {
  constructor(originalMethod, batchHelper) {
    __publicField(this, "batchSize", 2);
    __publicField(this, "batchTime", 200);
    __publicField(this, "scheduler", {});
    __publicField(this, "originalMethod");
    __publicField(this, "batchHelper");
    this.originalMethod = originalMethod;
    this.batchHelper = batchHelper;
  }
  addMessage(originContent) {
    if (document.hidden) {
      return;
    }
    const batchKey = this.batchHelper.getBatchKey(originContent.content);
    if (batchKey == null) {
      this.originalMethod(originContent);
      return;
    }
    if (!this.scheduler[batchKey]) {
      this.scheduler[batchKey] = {
        firedHandlers: {},
        pendingMessages: {}
      };
    }
    const messageStore = this.scheduler[batchKey];
    const { firedHandlers, pendingMessages, groupedCtx } = messageStore;
    if (groupedCtx) {
      groupedCtx.count++;
      this.applyContent(batchKey, originContent, messageStore);
      return;
    }
    const messageCount = Object.keys(firedHandlers).length + Object.keys(pendingMessages).length;
    if (messageCount + 1 > this.batchSize) {
      for (const k in firedHandlers) {
        try {
          firedHandlers[k]();
        } catch (e) {
        }
        delete firedHandlers[k];
      }
      for (const k in pendingMessages) {
        delete pendingMessages[k];
      }
      messageStore.groupedCtx = {
        key: originContent.key,
        count: messageCount + 1
      };
      this.applyContent(batchKey, originContent, messageStore);
      return;
    }
    pendingMessages[originContent.key] = originContent;
    setTimeout(() => {
      if (originContent.key in pendingMessages) {
        delete pendingMessages[originContent.key];
        originContent.onClose = () => {
          delete firedHandlers[originContent.key];
        };
        const handler = this.originalMethod(originContent);
        firedHandlers[originContent.key] = handler;
      }
    }, this.batchTime);
  }
  applyContent(batchKey, content, store) {
    content.content = this.batchHelper.getBatchContent(
      batchKey,
      store.groupedCtx.count
    );
    content.key = store.groupedCtx.key;
    content.onClose = () => {
      delete store.groupedCtx;
    };
    this.originalMethod(content);
  }
}
function createBatchMessageMethods(message, batchHelper) {
  let _message = __spreadValues({}, message);
  const methods = [
    "success",
    "error",
    "info",
    "warning",
    "warn",
    "loading"
  ];
  for (const method of methods) {
    const originalMethod = _message[method];
    const batcher = new Batcher(originalMethod, batchHelper);
    _message[method] = function(...args) {
      const key = makeUUID();
      const content = normalizeContent(args, method);
      batcher.addMessage(__spreadProps(__spreadValues({}, content), { key }));
      return () => {
      };
    };
  }
  return _message;
}
function normalizeContent(args, type) {
  const c = args[0];
  if (c && typeof c === "object" && "content" in c) {
    return c;
  }
  return {
    content: c,
    duration: typeof args[1] === "number" ? args[1] : DEFAULT_DURATION,
    type: type === "warn" ? "warning" : type
  };
}

export { Batcher, createBatchMessageMethods, makeUUID };
