import { I as Identifier } from './index-265fba1f.js';

class ClientIdentifier {
  constructor() { }
  static get value() {
    ClientIdentifier.valueCache = localStorage.getItem("clientIdentifier") || undefined;
    if (!ClientIdentifier.valueCache) {
      ClientIdentifier.valueCache = Identifier.generate(12);
      localStorage.setItem("clientIdentifier", ClientIdentifier.valueCache);
    }
    return ClientIdentifier.valueCache;
  }
}

class Message {
  static is(value) {
    return typeof value == "object" && typeof value.destination == "string" && value.content != undefined;
  }
  static send(message, content, context) {
    if (Message.is(message) && context == undefined) {
      context = content;
      if (!context)
        context = window;
      const destination = message.destination.split("#", 2);
      message = { destination: destination[1], content: message.content };
      context.postMessage(message, destination[0]);
    }
    else if (typeof context != "string") {
      if (!context)
        context = window;
      if (typeof message == "string")
        Message.send({ destination: message, content }, context);
    }
  }
  static listen(origin, handle, context) {
    const splitted = origin.split("#", 2);
    let destination = "";
    if (splitted.length == 2) {
      origin = splitted[0];
      destination = splitted[1];
    }
    ;
    (context || window).addEventListener("message", (e) => {
      const message = e.data;
      if (Message.is(message) &&
        (origin == "*" || e.origin == origin) &&
        (destination == "" || message.destination == destination))
        handle(message.destination, message.content);
    });
  }
}

class Notice {
  constructor(stateValue, messageValue, task) {
    this.stateValue = stateValue;
    this.messageValue = messageValue;
    this.task = task;
    this.listeners = [];
    switch (stateValue) {
      case "delayed":
        this.timer = window.setTimeout(() => this.execute(), 5000);
        break;
      case "executing":
        this.execute();
        break;
      case "success":
      case "warning":
      case "failed":
        this.startCloseTimer();
        break;
      default:
        break;
    }
  }
  get state() {
    return this.stateValue;
  }
  set state(value) {
    if (this.stateValue != value) {
      this.stateValue = value;
      this.emit();
    }
  }
  get message() {
    return this.messageValue;
  }
  set message(value) {
    if (this.messageValue != value) {
      this.messageValue = value;
      this.emit();
    }
  }
  listen(listener) {
    this.listeners.push(listener);
  }
  unlisten(listener) {
    this.listeners.filter(l => l != listener);
  }
  emit() {
    this.listeners.forEach(l => l(this));
  }
  execute() {
    this.clearTimer();
    if (this.task) {
      this.state = "executing";
      this.task(this).then(r => {
        this.messageValue = r[1];
        this.state = r[0] ? "success" : "failed";
        this.startCloseTimer();
      });
    }
    else
      this.state = "failed";
  }
  startCloseTimer() {
    this.timer = window.setTimeout(() => this.close(), 15000);
  }
  close() {
    this.clearTimer();
    this.state = "closed";
  }
  clearTimer() {
    if (this.timer) {
      window.clearTimeout(this.timer);
      this.timer = undefined;
    }
  }
  static delay(message, task) {
    return new Notice("delayed", message, task);
  }
  static execute(message, task) {
    return new Notice("executing", message, task);
  }
  static succeded(message) {
    return new Notice("success", message);
  }
  static failed(message) {
    return new Notice("failed", message);
  }
  static warn(message) {
    return new Notice("warning", message);
  }
}

class Trigger {
  static is(value) {
    return typeof value == "object" && typeof value.name == "string";
  }
}

export { Message as M, Notice as N, Trigger as T };
