{"version":3,"sources":["../src/state.ts"],"sourcesContent":["import { nanoid } from \"nanoid\";\nimport {\n  SubscriberFn,\n  Toast,\n  ToastIndicator,\n  ToastInternalProperties,\n  Toasts,\n} from \"./types\";\n\nclass Observable {\n  private toasts: Toasts;\n  private subscribers: Array<SubscriberFn>;\n\n  get globalToasts() {\n    return this.toasts;\n  }\n\n  public constructor() {\n    this.toasts = [];\n    this.subscribers = [];\n  }\n\n  /**\n   * The function allows entities to register a subscriber that\n   * allows entities to listen to changes in state.\n   */\n  public subscribe(subscriberFn: SubscriberFn) {\n    this.subscribers.push(subscriberFn);\n  }\n\n  /**\n   * The function is responsible for notifying the subscriber\n   * that a new toast has been added.\n   *\n   * NOTE: the state is managed by the subscribers and independent of the\n   * global state.\n   *\n   */\n  public createToast(\n    toast: Omit<Toast, ToastInternalProperties>,\n    indicator: ToastIndicator\n  ) {\n    const { description, title } = toast;\n\n    const toastObject: Toast = {\n      id: nanoid(),\n      dismisable: false,\n\n      title,\n      indicator,\n      description,\n      duration: Infinity,\n    };\n\n    // Adding the toast to the global state.\n    this.toasts = [toastObject, ...this.toasts];\n\n    // Notifying the subscribers about the creation of the toast.\n    this.subscribers.forEach((subscriber) => {\n      subscriber(toastObject);\n    });\n  }\n}\nexport const state = new Observable();\n\nconst basicCreateFn =\n  (indicator: ToastIndicator = ToastIndicator.info) =>\n  (toast: Omit<Toast, ToastInternalProperties>) =>\n    state.createToast(toast, indicator);\n\nexport const toast = Object.assign(basicCreateFn(), {\n  info: basicCreateFn(ToastIndicator.info),\n  warn: basicCreateFn(ToastIndicator.warn),\n  error: basicCreateFn(ToastIndicator.error),\n  success: basicCreateFn(ToastIndicator.success),\n});\n"],"mappings":"AAAA,OAAS,UAAAA,MAAc,SASvB,IAAMC,EAAN,KAAiB,CAIf,IAAI,cAAe,CACjB,OAAO,KAAK,MACd,CAEO,aAAc,CACnB,KAAK,OAAS,CAAC,EACf,KAAK,YAAc,CAAC,CACtB,CAMO,UAAUC,EAA4B,CAC3C,KAAK,YAAY,KAAKA,CAAY,CACpC,CAUO,YACLC,EACAC,EACA,CACA,GAAM,CAAE,YAAAC,EAAa,MAAAC,CAAM,EAAIH,EAEzBI,EAAqB,CACzB,GAAIC,EAAO,EACX,WAAY,GAEZ,MAAAF,EACA,UAAAF,EACA,YAAAC,EACA,SAAU,GACZ,EAGA,KAAK,OAAS,CAACE,EAAa,GAAG,KAAK,MAAM,EAG1C,KAAK,YAAY,QAASE,GAAe,CACvCA,EAAWF,CAAW,CACxB,CAAC,CACH,CACF,EACaG,EAAQ,IAAIT,EAEnBU,EACJ,CAACP,WACAD,GACCO,EAAM,YAAYP,EAAOC,CAAS,EAEzBD,EAAQ,OAAO,OAAOQ,EAAc,EAAG,CAClD,KAAMA,QAAiC,EACvC,KAAMA,QAAiC,EACvC,MAAOA,SAAkC,EACzC,QAASA,WAAoC,CAC/C,CAAC","names":["nanoid","Observable","subscriberFn","toast","indicator","description","title","toastObject","nanoid","subscriber","state","basicCreateFn"]}