{"version":3,"file":"edsis-component-toast.mjs","sources":["../../../library/component/toast/toast.service.ts","../../../library/component/toast/edsis-component-toast.ts"],"sourcesContent":["import { DOCUMENT } from '@angular/common';\nimport { Service, inject } from '@angular/core';\n\nexport type ToastVariant = 'default' | 'destructive' | 'success' | 'info' | 'warning';\n\nexport type ToastMessage<T> = string | ((value: T) => string);\n\nexport interface ToastPromiseMessages<T> {\n  readonly loading: string;\n  readonly success: ToastMessage<T>;\n  readonly error: ToastMessage<unknown>;\n}\n\nexport type ToastHorizontalPosition = 'start' | 'center' | 'end' | 'left' | 'right';\nexport type ToastVerticalPosition = 'top' | 'bottom';\n\nexport interface ToastOptions {\n  readonly title?: string;\n  readonly description?: string;\n  readonly action?: string;\n  readonly variant?: ToastVariant;\n  readonly durationMs?: number | null;\n  readonly horizontalPosition?: ToastHorizontalPosition;\n  readonly verticalPosition?: ToastVerticalPosition;\n}\n\n/** Handle for a shown toast. */\nexport interface ToastRef {\n  dismiss(): void;\n  /** Resolves with `true` when the action button dismissed the toast. */\n  readonly afterDismissed: Promise<{ dismissedByAction: boolean }>;\n}\n\nconst DEFAULT_TOAST_DURATION_MS = 5000;\n\nconst VARIANT_CLASSES: Record<ToastVariant, string> = {\n  default: 'border-border bg-background text-foreground',\n  destructive: 'border-destructive/50 bg-destructive text-destructive-foreground',\n  success: 'border-emerald-500/40 bg-emerald-600 text-white',\n  info: 'border-sky-500/40 bg-sky-600 text-white',\n  warning: 'border-amber-500/40 bg-amber-500 text-black',\n};\n\nconst resolveToastMessage = <T>(message: ToastMessage<T>, value: T): string =>\n  typeof message === 'function' ? message(value) : message;\n\n/**\n * Signal-era toast tanpa dependensi Material: stack dirender ke sebuah live\n * region (`role=\"status\"`, `aria-live=\"polite\"`) sehingga screen reader\n * mengumumkan setiap toast.\n */\n@Service()\nexport class ToastService {\n  private readonly documentRef = inject(DOCUMENT, { optional: true });\n  private container: HTMLElement | null = null;\n  private readonly active = new Set<HTMLElement>();\n\n  show(options: ToastOptions): ToastRef {\n    const doc = this.documentRef;\n\n    if (!doc) {\n      return { dismiss: () => {}, afterDismissed: Promise.resolve({ dismissedByAction: false }) };\n    }\n\n    const variant = options.variant ?? 'default';\n    const container = this.ensureContainer(doc, options);\n\n    const toast = doc.createElement('div');\n    toast.className = [\n      'toast-panel pointer-events-auto flex w-80 items-start gap-3 rounded-md border p-4 shadow-lg',\n      `toast-${variant}`,\n      VARIANT_CLASSES[variant],\n    ].join(' ');\n    toast.setAttribute('data-variant', variant);\n\n    const body = doc.createElement('div');\n    body.className = 'flex min-w-0 flex-1 flex-col gap-1';\n\n    if (options.title) {\n      const title = doc.createElement('p');\n      title.className = 'text-sm font-semibold leading-tight';\n      title.textContent = options.title;\n      body.appendChild(title);\n    }\n\n    if (options.description) {\n      const description = doc.createElement('p');\n      description.className = 'text-sm leading-snug opacity-90';\n      description.textContent = options.description;\n      body.appendChild(description);\n    }\n\n    toast.appendChild(body);\n\n    let resolveDismiss: (result: { dismissedByAction: boolean }) => void = () => {};\n    const afterDismissed = new Promise<{ dismissedByAction: boolean }>((resolve) => {\n      resolveDismiss = resolve;\n    });\n\n    let timeoutId: number | null = null;\n\n    const dismiss = (dismissedByAction = false): void => {\n      if (timeoutId !== null) {\n        clearTimeout(timeoutId);\n        timeoutId = null;\n      }\n      if (!this.active.delete(toast)) {\n        return;\n      }\n      toast.remove();\n      if (this.active.size === 0) {\n        this.container?.remove();\n        this.container = null;\n      }\n      resolveDismiss({ dismissedByAction });\n    };\n\n    if (options.action) {\n      const action = doc.createElement('button');\n      action.type = 'button';\n      action.className =\n        'shrink-0 rounded-md border border-current/30 px-2 py-1 text-xs font-medium transition-opacity hover:opacity-80';\n      action.textContent = options.action;\n      action.addEventListener('click', () => dismiss(true), { once: true });\n      toast.appendChild(action);\n    }\n\n    container.appendChild(toast);\n    this.active.add(toast);\n\n    const duration =\n      options.durationMs === null ? null : (options.durationMs ?? DEFAULT_TOAST_DURATION_MS);\n    if (duration !== null) {\n      timeoutId = (doc.defaultView ?? globalThis).setTimeout(\n        () => dismiss(false),\n        duration,\n      ) as unknown as number;\n    }\n\n    return { dismiss: () => dismiss(false), afterDismissed };\n  }\n\n  success(opts: Omit<ToastOptions, 'variant'>): ToastRef {\n    return this.show({ ...opts, variant: 'success' });\n  }\n\n  info(opts: Omit<ToastOptions, 'variant'>): ToastRef {\n    return this.show({ ...opts, variant: 'info' });\n  }\n\n  warning(opts: Omit<ToastOptions, 'variant'>): ToastRef {\n    return this.show({ ...opts, variant: 'warning' });\n  }\n\n  error(opts: Omit<ToastOptions, 'variant'>): ToastRef {\n    return this.show({ ...opts, variant: 'destructive' });\n  }\n\n  async promise<T>(\n    taskOrFactory: Promise<T> | (() => Promise<T>),\n    messages: ToastPromiseMessages<T>,\n  ): Promise<T> {\n    const loading = this.show({ title: messages.loading, durationMs: null });\n\n    try {\n      const task = typeof taskOrFactory === 'function' ? taskOrFactory() : taskOrFactory;\n      const value = await task;\n      loading.dismiss();\n      this.success({ title: resolveToastMessage(messages.success, value) });\n      return value;\n    } catch (error: unknown) {\n      loading.dismiss();\n      this.error({ title: resolveToastMessage(messages.error, error) });\n      throw error;\n    }\n  }\n\n  dismiss(): void {\n    for (const toast of [...this.active]) {\n      this.active.delete(toast);\n      toast.remove();\n    }\n    this.container?.remove();\n    this.container = null;\n  }\n\n  private ensureContainer(doc: Document, options: ToastOptions): HTMLElement {\n    if (this.container?.isConnected) {\n      return this.container;\n    }\n\n    const container = doc.createElement('div');\n    container.className = 'toast-container pointer-events-none fixed z-50 flex flex-col gap-2 p-4';\n    container.setAttribute('role', 'status');\n    container.setAttribute('aria-live', 'polite');\n\n    const vertical = options.verticalPosition ?? 'bottom';\n    const horizontal = options.horizontalPosition ?? 'end';\n\n    container.style.setProperty(vertical === 'top' ? 'top' : 'bottom', '0');\n    if (horizontal === 'center') {\n      container.style.setProperty('left', '50%');\n      container.style.setProperty('transform', 'translateX(-50%)');\n    } else if (horizontal === 'start' || horizontal === 'left') {\n      container.style.setProperty('left', '0');\n    } else {\n      container.style.setProperty('right', '0');\n    }\n\n    doc.body.appendChild(container);\n    this.container = container;\n    return container;\n  }\n}\n","/**\n * Generated bundle index. Do not edit.\n */\n\nexport * from './index';\n"],"names":[],"mappings":";;;;AAiCA,MAAM,yBAAyB,GAAG,IAAI;AAEtC,MAAM,eAAe,GAAiC;AACpD,IAAA,OAAO,EAAE,6CAA6C;AACtD,IAAA,WAAW,EAAE,kEAAkE;AAC/E,IAAA,OAAO,EAAE,iDAAiD;AAC1D,IAAA,IAAI,EAAE,yCAAyC;AAC/C,IAAA,OAAO,EAAE,6CAA6C;CACvD;AAED,MAAM,mBAAmB,GAAG,CAAI,OAAwB,EAAE,KAAQ,KAChE,OAAO,OAAO,KAAK,UAAU,GAAG,OAAO,CAAC,KAAK,CAAC,GAAG,OAAO;AAE1D;;;;AAIG;MAEU,YAAY,CAAA;IACN,WAAW,GAAG,MAAM,CAAC,QAAQ,EAAE,EAAE,QAAQ,EAAE,IAAI,EAAE,CAAC;IAC3D,SAAS,GAAuB,IAAI;AAC3B,IAAA,MAAM,GAAG,IAAI,GAAG,EAAe;AAEhD,IAAA,IAAI,CAAC,OAAqB,EAAA;AACxB,QAAA,MAAM,GAAG,GAAG,IAAI,CAAC,WAAW;QAE5B,IAAI,CAAC,GAAG,EAAE;YACR,OAAO,EAAE,OAAO,EAAE,MAAK,EAAE,CAAC,EAAE,cAAc,EAAE,OAAO,CAAC,OAAO,CAAC,EAAE,iBAAiB,EAAE,KAAK,EAAE,CAAC,EAAE;QAC7F;AAEA,QAAA,MAAM,OAAO,GAAG,OAAO,CAAC,OAAO,IAAI,SAAS;QAC5C,MAAM,SAAS,GAAG,IAAI,CAAC,eAAe,CAAC,GAAG,EAAE,OAAO,CAAC;QAEpD,MAAM,KAAK,GAAG,GAAG,CAAC,aAAa,CAAC,KAAK,CAAC;QACtC,KAAK,CAAC,SAAS,GAAG;YAChB,6FAA6F;AAC7F,YAAA,CAAA,MAAA,EAAS,OAAO,CAAA,CAAE;Y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