<svelte:options
  customElement={{
    tag: 'm-toaster',
    props: {
      open: { reflect: true, type: 'Boolean', attribute: 'open' },
      closable: { reflect: true, type: 'Boolean', attribute: 'closable' },
      progress: { reflect: true, type: 'Boolean', attribute: 'progress' },
    },
  }}
/>

<script lang="ts">
  import { onMount, onDestroy } from 'svelte';
  import {
    InfoCircleFilled32,
    WarningCircleFilled32,
    CrossCircleFilled32,
    CheckCircleFilled32,
    Cross20,
  } from '@mozaic-ds/icons-svelte';
  /**
   * A toaster is a temporary notification that appears briefly on the screen to provide feedback or updates without interrupting the user’s workflow. It is commonly used for success messages, warnings, errors, or informational updates. Toasters can disappear automatically after a few seconds, be dismissed manually via a close button, or be removed when the user performs a relevant action. They typically include an icon, a short message, and an optional close button for better usability.
   *
   * @slot action - Use this slot to insert a button or a link in the toaster
   * @event update:open {CustomEvent<boolean>} - Fired when the open state changes.
   */
  interface Props {
    [key: string]: any;
    /**
     * If `true`, display the Toaster.
     */
    open?: boolean;
    /**
     * Position of the toaster.
     */
    position?: 'top' | 'bottom' | 'top-center' | 'bottom-center';
    /**
     * Description of the toaster.
     */
    description: string;
    /**
     * Allows to define the toaster style.
     */
    status?: 'info' | 'success' | 'warning' | 'error';
    /**
     * If `true`, display the close button.
     */
    closable?: boolean;
    /**
     * If `true`, display the progress bar of the duration.
     */
    progress?: boolean;
    /**
     * Duration of the toaster
     */
    timeout?: number;
    /**
     * Callback fired when the toaster open state changes.
     */
    onUpdateOpen?: (open: boolean) => void;
  }

  let {
    open,
    position = 'top',
    description,
    status = 'info',
    closable = true,
    progress,
    timeout,
    onUpdateOpen,
    class: className,
    ...attrs
  }: Props = $props();
  let element: HTMLElement;

  const iconMap = [
    { status: 'info', component: InfoCircleFilled32 },
    { status: 'success', component: CheckCircleFilled32 },
    { status: 'warning', component: WarningCircleFilled32 },
    { status: 'error', component: CrossCircleFilled32 },
  ];

  function getSelected(status: string) {
    return iconMap.find((icon) => icon.status === status) || iconMap[0];
  }

  const selected = $derived(getSelected(status));

  let progressValue = $state(0);
  let intervalId: ReturnType<typeof setInterval> | null = null;
  let timeoutId: ReturnType<typeof setTimeout> | null = null;

  const onClose = () => {
    open = false;

    onUpdateOpen?.(open);

    const event = new CustomEvent('update:open', {
      detail: open,
      bubbles: true,
      composed: true,
    });
    element.dispatchEvent(event);
  };

  onMount(() => {
    if (timeout) {
      if (progress) {
        const intervalDuration = 100;
        const steps = timeout / intervalDuration;
        const increment = 100 / steps;

        intervalId = setInterval(() => {
          progressValue += increment;
          if (progressValue >= 100) {
            progressValue = 100;
            clearInterval(intervalId!);
            onClose();
          }
        }, intervalDuration);
      } else {
        timeoutId = setTimeout(() => {
          onClose();
        }, timeout);
      }
    }
  });

  onDestroy(() => {
    if (intervalId) clearInterval(intervalId);
    if (timeoutId) clearTimeout(timeoutId);
  });
</script>

<section
  class={[
    'mc-toaster',
    open && 'is-open',
    `mc-toaster--${status}`,
    `mc-toaster--${position}`,
    className,
  ]}
  role="status"
  tabindex="-1"
  aria-hidden={!open}
  bind:this={element}
  {...attrs}
>
  <span class="mc-toaster__icon">
    <selected.component />
  </span>

  <div class="mc-toaster__content">
    <p class="mc-toaster__message">{description}</p>

    <div class="mc-toaster__action">
      <slot name="action" />
    </div>
  </div>

  {#if closable}
    <button
      class="mc-toaster__close mc-button mc-button--icon-button mc-button--inverse mc-button--s mc-button--ghost"
      type="button"
      aria-label="Close"
      onclick={onClose}
    >
      <Cross20 className="mc-button__icon" slot="icon" />
    </button>
  {/if}

  {#if progress}
    <div class="mc-toaster__indicator mc-linear-progressbar-buffer mc-linear-progressbar-buffer--m">
      <div
        class="mc-linear-progressbar-buffer__indicator"
        role="progressbar"
        style={`--progress-value: ${progressValue};`}
        aria-valuenow={progressValue}
        aria-valuemin="0"
        aria-valuemax="100"
      ></div>
    </div>
  {/if}
</section>

<style>/**
 * Do not edit directly, this file was auto-generated.
 */
.mc-button {
  color: var(--button-color-filled-standard-font, #ffffff);
  background-color: var(--button-color-filled-standard-background, #464e63);
}
.mc-button:hover {
  background-color: var(--button-color-filled-standard-hover-background, #343b4c);
}
.mc-button:active {
  background-color: var(--button-color-filled-standard-active-background, #242938);
}
.mc-button:disabled {
  background-color: var(--button-state-disabled-background, #d9d9d9);
  border-color: transparent;
  color: var(--button-state-disabled-color, #737373);
  cursor: not-allowed;
}
.mc-button {
  font-weight: var(--font-weight-semi-bold, 600);
  padding: 0 calc(1rem - 0.125rem);
  min-height: 3rem;
  min-width: 3rem;
  border-radius: var(--button-border-radius-m, 0.25rem);
}
.mc-button .mc-button__label {
  font-size: var(--font-size-150, 1rem);
}
.mc-button {
  display: inline-flex;
  justify-content: center;
  vertical-align: middle;
  text-align: center;
  border: 2px solid transparent;
  transition: all ease 200ms;
  transition: box-shadow 200ms ease;
  align-items: center;
  box-sizing: border-box;
  font-family: inherit;
  fill: currentcolor;
  gap: 0.25rem;
  cursor: pointer;
}
.mc-button__label {
  font-size: var(--font-size-150, 1rem);
}
.mc-button__icon {
  flex-shrink: 0;
  width: 1.5rem;
  height: 1.5rem;
}
.mc-button:disabled {
  background-color: var(--button-state-disabled-background, #d9d9d9);
  border-color: transparent;
  color: var(--button-state-disabled-color, #737373);
  cursor: not-allowed;
}
.mc-button:focus-visible {
  box-shadow: 0 0 0 0.125rem var(--focus-color-mid, var(--focus-color-outline-mid, #ffffff)), 0 0 0 0.25rem var(--focus-color-outer, var(--focus-color-outline-outer, #000000));
  outline: 0.125rem solid transparent;
  outline-offset: 0.125rem;
}
.mc-button--s {
  padding: 0 calc(0.75rem - 0.125rem);
  min-height: 2rem;
  min-width: 2rem;
  border-radius: var(--button-border-radius-s, 0.25rem);
}
.mc-button--s .mc-button__label {
  font-size: var(--font-size-100, 0.875rem);
}
.mc-button--s .mc-button__icon {
  width: 1.25rem;
  height: 1.25rem;
}
.mc-button--s .mc-button__icon:only-child {
  width: 1.25rem;
  height: 1.25rem;
}
.mc-button--m {
  padding: 0 calc(1rem - 0.125rem);
  min-height: 3rem;
  min-width: 3rem;
  border-radius: var(--button-border-radius-m, 0.25rem);
}
.mc-button--m .mc-button__label {
  font-size: var(--font-size-150, 1rem);
}
.mc-button--m .mc-button__icon {
  width: 1.5rem;
  height: 1.5rem;
}
.mc-button--m .mc-button__icon:only-child {
  width: 1.5rem;
  height: 1.5rem;
}
.mc-button--l {
  padding: 0 calc(1.25rem - 0.125rem);
  min-height: 4rem;
  min-width: 4rem;
  border-radius: var(--button-border-radius-l, 0.25rem);
}
.mc-button--l .mc-button__label {
  font-size: var(--font-size-200, 1.125rem);
}
.mc-button--l .mc-button__icon {
  width: 2rem;
  height: 2rem;
}
.mc-button--l .mc-button__icon:only-child {
  width: 2rem;
  height: 2rem;
}
.mc-button--icon-only {
  padding: 0.25rem;
}
.mc-button--outlined {
  color: var(--button-color-outlined-standard-font, #242938);
  border-color: var(--button-color-outlined-standard-border, #8891aa);
  background-color: var(--button-color-outlined-standard-background, #ffffff);
}
.mc-button--outlined:hover {
  background-color: var(--button-color-outlined-standard-hover-background, #eff1f6);
}
.mc-button--outlined:active {
  background-color: var(--button-color-outlined-standard-active-background, #c9d0de);
}
.mc-button--outlined:disabled {
  background-color: var(--button-state-disabled-background, #d9d9d9);
  border-color: transparent;
  color: var(--button-state-disabled-color, #737373);
  cursor: not-allowed;
}
.mc-button--ghost {
  color: var(--button-color-ghost-standard-font, #242938);
  background-color: var(--button-color-ghost-standard-background, rgba(255, 255, 255, 0.01));
}
.mc-button--ghost:hover {
  background-color: var(--button-color-ghost-standard-hover-background, rgba(70, 78, 99, 0.1));
}
.mc-button--ghost:active {
  background-color: var(--button-color-ghost-standard-active-background, rgba(70, 78, 99, 0.2));
}
.mc-button--ghost:disabled {
  background-color: var(--button-state-disabled-background, #d9d9d9);
  border-color: transparent;
  color: var(--button-state-disabled-color, #737373);
  cursor: not-allowed;
}
.mc-button--accent {
  color: var(--button-color-filled-accent-font, #ffffff);
  background-color: var(--button-color-filled-accent-background, #117f03);
}
.mc-button--accent:hover {
  background-color: var(--button-color-filled-accent-hover-background, #006902);
}
.mc-button--accent:active {
  background-color: var(--button-color-filled-accent-active-background, #035010);
}
.mc-button--accent:disabled {
  background-color: var(--button-state-disabled-background, #d9d9d9);
  border-color: transparent;
  color: var(--button-state-disabled-color, #737373);
  cursor: not-allowed;
}
.mc-button--danger {
  color: var(--button-color-filled-danger-font, #ffffff);
  background-color: var(--button-color-filled-danger-background, #c61112);
}
.mc-button--danger:hover {
  background-color: var(--button-color-filled-danger-hover-background, #8c0003);
}
.mc-button--danger:active {
  background-color: var(--button-color-filled-danger-active-background, #530000);
}
.mc-button--danger:disabled {
  background-color: var(--button-state-disabled-background, #d9d9d9);
  border-color: transparent;
  color: var(--button-state-disabled-color, #737373);
  cursor: not-allowed;
}
.mc-button--inverse {
  --focus-color-mid: var(--focus-color-outline-outer, #000000);
  --focus-color-outer: var(--focus-color-outline-mid, #ffffff);
  color: var(--button-color-filled-inverse-font, #242938);
  background-color: var(--button-color-filled-inverse-background, #ffffff);
}
.mc-button--inverse:hover {
  background-color: var(--button-color-filled-inverse-hover-background, #e6e6e6);
}
.mc-button--inverse:active {
  background-color: var(--button-color-filled-inverse-active-background, #cccccc);
}
.mc-button--inverse:disabled {
  background-color: var(--button-state-disabled-background, #d9d9d9);
  border-color: transparent;
  color: var(--button-state-disabled-color, #737373);
  cursor: not-allowed;
}
.mc-button--icon-button {
  border-radius: var(--border-radius-full, 100%);
  padding: 0;
}
.mc-button--loading {
  pointer-events: none;
}
.mc-button--loading .mc-button__loader {
  position: absolute;
  color: currentcolor;
}
.mc-button--loading .mc-button__label,
.mc-button--loading .mc-button__icon {
  visibility: hidden;
}
.mc-button--outlined.mc-button--standard {
  color: var(--button-color-outlined-standard-font, #242938);
  border-color: var(--button-color-outlined-standard-border, #8891aa);
  background-color: var(--button-color-outlined-standard-background, #ffffff);
}
.mc-button--outlined.mc-button--standard:hover {
  background-color: var(--button-color-outlined-standard-hover-background, #eff1f6);
}
.mc-button--outlined.mc-button--standard:active {
  background-color: var(--button-color-outlined-standard-active-background, #c9d0de);
}
.mc-button--outlined.mc-button--standard:disabled {
  background-color: var(--button-state-disabled-background, #d9d9d9);
  border-color: transparent;
  color: var(--button-state-disabled-color, #737373);
  cursor: not-allowed;
}
.mc-button--outlined.mc-button--accent {
  color: var(--button-color-outlined-accent-font, #117f03);
  border-color: var(--button-color-outlined-accent-border, #78be20);
  background-color: var(--button-color-outlined-accent-background, #ffffff);
}
.mc-button--outlined.mc-button--accent:hover {
  background-color: var(--button-color-outlined-accent-hover-background, #ebf5de);
}
.mc-button--outlined.mc-button--accent:active {
  background-color: var(--button-color-outlined-accent-active-background, #c5e39e);
}
.mc-button--outlined.mc-button--accent:disabled {
  background-color: var(--button-state-disabled-background, #d9d9d9);
  border-color: transparent;
  color: var(--button-state-disabled-color, #737373);
  cursor: not-allowed;
}
.mc-button--outlined.mc-button--danger {
  color: var(--button-color-outlined-danger-font, #c61112);
  border-color: var(--button-color-outlined-danger-border, #ef5f5c);
  background-color: var(--button-color-outlined-danger-background, #ffffff);
}
.mc-button--outlined.mc-button--danger:hover {
  background-color: var(--button-color-outlined-danger-hover-background, #fdeaea);
}
.mc-button--outlined.mc-button--danger:active {
  background-color: var(--button-color-outlined-danger-active-background, #f8bcbb);
}
.mc-button--outlined.mc-button--danger:disabled {
  background-color: var(--button-state-disabled-background, #d9d9d9);
  border-color: transparent;
  color: var(--button-state-disabled-color, #737373);
  cursor: not-allowed;
}
.mc-button--outlined.mc-button--inverse {
  color: var(--button-color-outlined-inverse-font, #ffffff);
  border-color: var(--button-color-outlined-inverse-border, #ffffff);
  background-color: var(--button-color-outlined-inverse-background, rgba(255, 255, 255, 0.01));
}
.mc-button--outlined.mc-button--inverse:hover {
  background-color: var(--button-color-outlined-inverse-hover-background, rgba(255, 255, 255, 0.1));
}
.mc-button--outlined.mc-button--inverse:active {
  background-color: var(--button-color-outlined-inverse-active-background, rgba(255, 255, 255, 0.2));
}
.mc-button--outlined.mc-button--inverse:disabled {
  background-color: var(--button-state-disabled-background, #d9d9d9);
  border-color: transparent;
  color: var(--button-state-disabled-color, #737373);
  cursor: not-allowed;
}
.mc-button--ghost.mc-button--standard {
  color: var(--button-color-ghost-standard-font, #242938);
  background-color: var(--button-color-ghost-standard-background, rgba(255, 255, 255, 0.01));
}
.mc-button--ghost.mc-button--standard:hover {
  background-color: var(--button-color-ghost-standard-hover-background, rgba(70, 78, 99, 0.1));
}
.mc-button--ghost.mc-button--standard:active {
  background-color: var(--button-color-ghost-standard-active-background, rgba(70, 78, 99, 0.2));
}
.mc-button--ghost.mc-button--standard:disabled {
  background-color: var(--button-state-disabled-background, #d9d9d9);
  border-color: transparent;
  color: var(--button-state-disabled-color, #737373);
  cursor: not-allowed;
}
.mc-button--ghost.mc-button--accent {
  color: var(--button-color-ghost-accent-font, #117f03);
  background-color: var(--button-color-ghost-accent-background, rgba(255, 255, 255, 0.01));
}
.mc-button--ghost.mc-button--accent:hover {
  background-color: var(--button-color-ghost-accent-hover-background, rgba(17, 127, 3, 0.1));
}
.mc-button--ghost.mc-button--accent:active {
  background-color: var(--button-color-ghost-accent-active-background, rgba(17, 127, 3, 0.2));
}
.mc-button--ghost.mc-button--accent:disabled {
  background-color: var(--button-state-disabled-background, #d9d9d9);
  border-color: transparent;
  color: var(--button-state-disabled-color, #737373);
  cursor: not-allowed;
}
.mc-button--ghost.mc-button--danger {
  color: var(--button-color-ghost-danger-font, #c61112);
  background-color: var(--button-color-ghost-danger-background, rgba(255, 255, 255, 0.01));
}
.mc-button--ghost.mc-button--danger:hover {
  background-color: var(--button-color-ghost-danger-hover-background, rgba(198, 17, 18, 0.1));
}
.mc-button--ghost.mc-button--danger:active {
  background-color: var(--button-color-ghost-danger-active-background, rgba(198, 17, 18, 0.2));
}
.mc-button--ghost.mc-button--danger:disabled {
  background-color: var(--button-state-disabled-background, #d9d9d9);
  border-color: transparent;
  color: var(--button-state-disabled-color, #737373);
  cursor: not-allowed;
}
.mc-button--ghost.mc-button--inverse {
  color: var(--button-color-ghost-inverse-font, #ffffff);
  background-color: var(--button-color-ghost-inverse-background, rgba(255, 255, 255, 0.01));
}
.mc-button--ghost.mc-button--inverse:hover {
  background-color: var(--button-color-ghost-inverse-hover-background, rgba(255, 255, 255, 0.1));
}
.mc-button--ghost.mc-button--inverse:active {
  background-color: var(--button-color-ghost-inverse-active-background, rgba(255, 255, 255, 0.2));
}
.mc-button--ghost.mc-button--inverse:disabled {
  background-color: var(--button-state-disabled-background, #d9d9d9);
  border-color: transparent;
  color: var(--button-state-disabled-color, #737373);
  cursor: not-allowed;
}

.mc-linear-progressbar-buffer {
  --progress-value: 40;
  background-color: var(--progressbar-color-background, #c9d0de);
  height: 0.25rem;
  position: relative;
  min-width: 6rem;
  border-radius: var(--border-radius-l, 1rem);
}
.mc-linear-progressbar-buffer__indicator {
  background-color: var(--progressbar-color-indicator, #464e63);
  height: 100%;
  transition: width 0.4s ease;
  overflow: hidden;
  border-radius: var(--border-radius-l, 1rem);
  box-sizing: border-box;
  width: calc(var(--progress-value) * 1%);
}
.mc-linear-progressbar-buffer--s {
  height: 0.125rem;
}
.mc-linear-progressbar-buffer--l {
  height: 0.5rem;
}

.mc-toaster {
  border-radius: var(--border-radius-m, 0.5rem);
  background: var(--toaster-color-background-info, #002a41);
  display: flex;
  flex-flow: row wrap;
  align-items: flex-start;
  min-height: 3.5rem;
  min-width: 18rem;
  position: absolute;
  top: 1.5rem;
  right: 1.5rem;
  margin-left: 1.5rem;
  box-sizing: border-box;
  color: var(--toaster-color-text, #ffffff);
  z-index: var(--toaster-z-index, 6);
  opacity: 0;
  visibility: hidden;
  transform: translateX(25%);
  transition: visibility 0s linear 0.4s, transform 0.4s ease-in-out, opacity 0.4s ease-in-out;
}
.mc-toaster.is-open {
  opacity: 1;
  visibility: visible;
  transform: translateX(0);
}
.mc-toaster__icon {
  width: 2rem;
  height: 2rem;
  margin: 0.75rem;
  fill: var(--toaster-color-icon-info, #3facd7);
  color: var(--toaster-color-icon-info, #3facd7);
}
.mc-toaster__content {
  flex: 1 1 0;
  flex-direction: column;
  align-self: stretch;
  display: flex;
  align-items: flex-start;
  gap: 1rem;
  padding: 0.75rem 1rem 0.75rem 0;
}
@media (width >= 680px) {
  .mc-toaster__content {
    flex-direction: row;
    align-items: center;
  }
}
.mc-toaster__message {
  flex: 1 1 0;
  margin: 0;
  font-size: var(--font-size-150, 1rem);
  line-height: var(--line-height-s, 1.3);
}
.mc-toaster--information {
  background: var(--toaster-color-background-info, #002a41);
}
.mc-toaster--information .mc-toaster__icon {
  fill: var(--toaster-color-icon-info, #3facd7);
  color: var(--toaster-color-icon-info, #3facd7);
}
.mc-toaster--information .mc-toaster__indicator {
  --progressbar-color-indicator: var(--toaster-color-icon-info, #3facd7);
}
.mc-toaster--success {
  background: var(--toaster-color-background-success, #023618);
}
.mc-toaster--success .mc-toaster__icon {
  fill: var(--toaster-color-icon-success, #78be20);
  color: var(--toaster-color-icon-success, #78be20);
}
.mc-toaster--success .mc-toaster__indicator {
  --progressbar-color-indicator: var(--toaster-color-icon-success, #78be20);
}
.mc-toaster--warning {
  background: var(--toaster-color-background-warning, #360e00);
}
.mc-toaster--warning .mc-toaster__icon {
  fill: var(--toaster-color-icon-warning, #ef934a);
  color: var(--toaster-color-icon-warning, #ef934a);
}
.mc-toaster--warning .mc-toaster__indicator {
  --progressbar-color-indicator: var(--toaster-color-icon-warning, #ef934a);
}
.mc-toaster--error {
  background: var(--toaster-color-background-error, #2d0000);
}
.mc-toaster--error .mc-toaster__icon {
  fill: var(--toaster-color-icon-error, #ef5f5c);
  color: var(--toaster-color-icon-error, #ef5f5c);
}
.mc-toaster--error .mc-toaster__indicator {
  --progressbar-color-indicator: var(--toaster-color-icon-error, #ef5f5c);
}
.mc-toaster--bottom {
  top: inherit;
  bottom: 1.5rem;
  right: 1.5rem;
}
.mc-toaster--top-center {
  top: 1.5rem;
  right: 50%;
  transform: translateX(50%) !important;
}
.mc-toaster--bottom-center {
  top: inherit;
  bottom: 1.5rem;
  right: 50%;
  transform: translateX(50%) !important;
}
.mc-toaster__indicator {
  width: 100%;
  margin: 0.5rem;
  --progressbar-color-indicator: var(--toaster-color-icon-info, #3facd7);
  --progressbar-color-background: var(--toaster-color-progress-bar-background, #ffffff);
}
.mc-toaster__close {
  margin: 0.25rem;
}

.mc-toaster__action:empty {
  display: none;
}</style>
