<script lang="ts">
  import { Cross24 } from '@mozaic-ds/icons-svelte';
  import MOverlay from '../overlay/Overlay.svelte';

  /**
   * A modal is a dialog window that appears on top of the main content, requiring user interaction before returning to the main interface. It is used to focus attention on a specific task, provide important information, or request confirmation for an action. Modals typically include a title, description, and primary/secondary actions and should be used for single, focused tasks to avoid disrupting the user experience.
   *
   * @slot icon - Use this slot to insert an icon next to the title of the modal.
   * @slot default - Use this slot to insert the content of the modal.
   * @slot link - Use this slot to insert a link in the footer.
   * @slot footer - Use this slot to insert buttons in the footer.
   * @event update:open {CustomEvent<boolean>} - Fired when the open state changes.
   */
  interface Props {
    [key: string]: any;

    /**
     * if `true`, display the modal.
     */
    open?: boolean;
    /**
     * Title of the modal.
     */
    title?: string;
    /**
     * Description of the modal.
     */
    description?: string;
    /**
     * if `true`, display the close button.
     */
    closable?: boolean;
    /**
     * Callback fired when the modal open state changes.
     */
    onUpdateOpen?: (open: boolean) => void;
    /**
     * Callback fired when the overlay is clicked.
     */
    onOverlayClick?: () => void;
  }

  let {
    open,
    title,
    description,
    closable = true,
    onUpdateOpen,
    onOverlayClick,
    class: className,
    ...attrs
  }: Props = $props();
  let element: HTMLElement;

  function onClose() {
    open = false;

    onUpdateOpen?.(open);

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

<section
  class={['mc-modal', open && 'is-open', className]}
  tabindex="-1"
  aria-labelledby="modalTitle"
  aria-hidden={!open}
  bind:this={element}
  {...attrs}
>
  <div class="mc-modal__dialog" role="document">
    <header class="mc-modal__header">
      <span class="mc-modal__icon">
        <slot name="icon" />
      </span>
      {#if title}
        <h2 class="mc-modal__title" id="modalTitle">{title}</h2>
      {/if}
      {#if closable}
        <button
          type="button"
          class="mc-modal__close mc-button mc-button--icon-button mc-button--ghost"
          aria-label="Close"
          onclick={onClose}
        >
          <Cross24 className="mc-modal__close" />
        </button>
      {/if}
    </header>

    <main class="mc-modal__body">
      {#if description}
        <p>{description}</p>
      {/if}
      <slot />
    </main>

    <footer class="mc-modal__footer">
      <span class="mc-modal__link">
        <slot name="link" />
      </span>
      <slot name="footer" />
    </footer>
  </div>

  <MOverlay isvisible={open} dialoglabel="modal" {onOverlayClick} />
</section>

<style>/**
 * Do not edit directly, this file was auto-generated.
 */
.mc-modal {
  box-sizing: border-box;
  inset: 0;
  outline: 0;
  pointer-events: none;
  position: fixed;
  z-index: var(--modal-z-index, 5);
  display: flex;
  justify-content: center;
  align-items: flex-end;
  overflow: hidden auto;
}
@media (width >= 680px) {
  .mc-modal {
    align-items: center;
  }
}
.mc-modal__dialog {
  background: var(--modal-color-background, #ffffff);
  display: flex;
  flex-direction: column;
  opacity: 0;
  position: relative;
  transform: translateY(-25%);
  transition: visibility 0s linear 0.4s, transform 0.4s ease, opacity 0.4s ease;
  visibility: hidden;
  width: 100%;
  border-radius: var(--border-radius-l, 1rem) var(--border-radius-l, 1rem) 0 0;
  z-index: var(--modal-z-index, 5);
}
@media (width >= 680px) {
  .mc-modal__dialog {
    width: 38rem;
    border-radius: var(--border-radius-l, 1rem);
  }
}
.mc-modal.is-open .mc-modal__dialog {
  opacity: 1;
  pointer-events: all;
  transform: translateY(0);
  transition: visibility 0s linear 0s, transform 0.4s ease, opacity 0.4s ease;
  visibility: visible;
}
.mc-modal__header {
  align-items: center;
  display: flex;
  gap: 0.5rem;
  min-height: 3rem;
  padding: 1rem;
  position: relative;
}
@media (width >= 680px) {
  .mc-modal__header {
    padding: 1.5rem 1.5rem 1rem 1.5rem;
  }
}
.mc-modal__icon {
  width: 2rem;
  height: 2rem;
  color: var(--modal-color-title, #000000);
}
.mc-modal__title {
  font-size: var(--font-title-m, 1.5rem);
  font-weight: var(--font-weight-semi-bold, 600);
  color: var(--modal-color-title, #000000);
  margin-bottom: 0.5rem;
  margin-top: 0.5rem;
  padding-right: 3rem;
}
.mc-modal__close {
  position: absolute;
  top: 0.75rem;
  right: 0.75rem;
}
.mc-modal__body {
  font-size: var(--font-size-150, 1rem);
  color: var(--modal-color-paragraph, #404040);
  padding: 0 1rem;
  margin-bottom: 1rem;
}
@media (width >= 680px) {
  .mc-modal__body {
    padding: 0 1.5rem;
  }
}
.mc-modal__body p {
  margin: 0;
  line-height: var(--line-height-s, 1.3);
}
.mc-modal__footer {
  align-items: stretch;
  display: flex;
  flex-direction: column-reverse;
  justify-content: flex-end;
  gap: 1rem;
  padding: 0.5rem 1rem 1rem 1rem;
}
@media (width >= 680px) {
  .mc-modal__footer {
    flex-direction: row;
    padding: 0.5rem 1.5rem 1.5rem 1.5rem;
  }
}
.mc-modal__link {
  order: 1;
}
@media (width >= 680px) {
  .mc-modal__link {
    margin-right: auto;
    order: 0;
  }
}
.mc-modal-open {
  overflow: hidden;
}

.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);
  color: var(--button-color-filled-standard-font, #ffffff);
}
.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 {
  box-shadow: none;
  outline: none;
}
.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);
  color: var(--button-color-outlined-standard-font, #242938);
}
.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));
  color: var(--button-color-ghost-standard-font, #242938);
}
.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);
  color: var(--button-color-filled-accent-font, #ffffff);
}
.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);
  color: var(--button-color-filled-danger-font, #ffffff);
}
.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);
  color: var(--button-color-filled-inverse-font, #242938);
}
.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);
  color: var(--button-color-outlined-standard-font, #242938);
}
.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);
  color: var(--button-color-outlined-accent-font, #117f03);
}
.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);
  color: var(--button-color-outlined-danger-font, #c61112);
}
.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));
  color: var(--button-color-outlined-inverse-font, #ffffff);
}
.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));
  color: var(--button-color-ghost-standard-font, #242938);
}
.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));
  color: var(--button-color-ghost-accent-font, #117f03);
}
.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));
  color: var(--button-color-ghost-danger-font, #c61112);
}
.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));
  color: var(--button-color-ghost-inverse-font, #ffffff);
}
.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-modal__icon:empty {
  display: none;
}</style>
