<script lang="ts">
  import { ArrowBack24, Cross24 } from '@mozaic-ds/icons-svelte';
  import { tick } from 'svelte';
  import MOverlay from '../overlay/Overlay.svelte';
  /**
   * A drawer is a sliding panel that appears from the side of the screen, providing additional content, settings, or actions without disrupting the main view. It is often used for filtering options, or contextual details. It enhances usability by keeping interfaces clean while offering expandable functionality.
   *
   * @slot default - Use this slot to insert the content of the drawer
   * @slot footer - Use this slot to insert buttons in the footer
   * @event update:open {CustomEvent<boolean>} - Emits when the drawer open state changes.
   * @event back {CustomEvent<void>} - Emits when click back button of the drawer.
   */
  interface Props {
    [key: string]: any;
    /**
     * If `true`, display the drawer.
     */
    open?: boolean;
    /**
     * Position of the drawer.
     */
    position?: 'left' | 'right';
    /**
     * If `true`, the drawer have a bigger width.
     */
    extended?: boolean;
    /**
     * If `true`, display the back button.
     */
    back?: boolean;
    /**
     * Title of the drawer.
     */
    title: string;
    /**
     * Title of the content of the drawer.
     */
    contentTitle?: string;
    /**
     * Callback fired when the drawer open state changes.
     */
    onUpdateOpen?: (open: boolean) => void;
    /**
     * Callback fired when the back button is clicked.
     */
    onBack?: () => void;
    /**
     * Callback fired when the overlay is clicked.
     */
    onOverlayClick?: () => void;
  }

  let {
    open,
    position,
    extended,
    back,
    title,
    contentTitle,
    onUpdateOpen,
    onBack,
    onOverlayClick,
    class: className,
    ...attrs
  }: Props = $props();
  let element: HTMLElement;
  let titleEl: HTMLHeadingElement | undefined = undefined;

  $effect(() => {
    if (!open) return;
    (async () => {
      await tick();
      titleEl?.focus({ preventScroll: true });
    })();
  });

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

    onUpdateOpen?.(open);

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

  const onClickBack = () => {
    onBack?.();

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

<div
  class={[
    'mc-drawer',
    `mc-drawer--${position}`,
    open && 'is-open',
    extended && 'mc-drawer--extend',
    className,
  ]}
  role="dialog"
  aria-labelledby="drawerTitle"
  aria-modal={open ? 'true' : 'false'}
  aria-hidden={!open}
  onkeydown={(e) => e.key === 'Escape' && onClose()}
  bind:this={element}
  {...attrs}
>
  <div class="mc-drawer__dialog" role="document">
    <div class="mc-drawer__header">
      {#if back}
        <button
          class="mc-drawer__back mc-button mc-button--icon-button mc-button--ghost"
          aria-label="Back"
          onclick={onClickBack}
        >
          <ArrowBack24 />
        </button>
      {/if}
      <h2 class="mc-drawer__title" id="drawerTitle" tabIndex="-1" bind:this={titleEl}>{title}</h2>
      <button
        class="mc-drawer__close mc-button mc-button--icon-button mc-button--ghost"
        aria-label="Close"
        onclick={onClose}
      >
        <Cross24 />
      </button>
    </div>

    <div class="mc-drawer__body">
      <div class="mc-drawer__content">
        {#if contentTitle}
          <h2 class="mc-drawer__content__title">{contentTitle}</h2>
        {/if}
        <slot />
      </div>
    </div>

    <div class="mc-drawer__footer">
      <slot name="footer" />
    </div>
  </div>
  <MOverlay isvisible={open} dialoglabel="drawerTitle" {onOverlayClick} />
</div>

<style>/**
 * Do not edit directly, this file was auto-generated.
 */
.mc-drawer {
  box-sizing: border-box;
  inset: 0;
  outline: 0;
  pointer-events: none;
  position: fixed;
  display: flex;
  overflow: hidden;
  padding: 0.5rem;
  justify-content: flex-end;
  z-index: var(--drawer-z-index, 5);
}
@media (width >= 680px) {
  .mc-drawer {
    padding: 1rem;
  }
}
.mc-drawer--left {
  justify-content: flex-start;
}
.mc-drawer--left .mc-drawer__dialog {
  transform: translate3d(-100%, 0, 0);
}
@media (width >= 680px) {
  .mc-drawer--extend .mc-drawer__dialog {
    max-width: 39rem;
  }
}
.mc-drawer__dialog {
  background: var(--drawer-color-background, #ffffff);
  display: flex;
  flex-direction: column;
  height: 100%;
  max-width: 100%;
  transform: translate3d(100%, 0, 0);
  transition: visibility 0s linear 0.4s, transform 0.4s;
  visibility: hidden;
  width: 100%;
  z-index: var(--drawer-z-index, 5);
  border-radius: var(--border-radius-l, 1rem);
}
@media (width >= 680px) {
  .mc-drawer__dialog {
    max-width: 25rem;
  }
}
.mc-drawer__header {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 4rem;
}
.mc-drawer__icon {
  height: 1.5rem;
  margin-right: 0.75rem;
  width: 1.5rem;
}
.mc-drawer__title {
  font-size: var(--font-body-s, 0.875rem);
  font-weight: var(--font-weight-semi-bold, 600);
  color: var(--drawer-color-title, #666666);
  margin-bottom: 0;
  margin-top: 0;
  text-align: center;
  flex-grow: 1;
}
.mc-drawer__title:first-child {
  padding-left: 4rem;
}
.mc-drawer__back, .mc-drawer__close {
  margin: 0.5rem;
}
.mc-drawer__body {
  flex: 1 1;
  overflow: hidden;
}
.mc-drawer__body:last-child {
  margin-bottom: 1.5rem;
  padding-bottom: 1.5rem;
}
.mc-drawer__content {
  max-height: 100%;
  overflow: hidden auto;
  padding: 0.5rem 1rem 0;
  color: var(--drawer-color-paragraph, #000000);
}
@media (width >= 680px) {
  .mc-drawer__content {
    padding: 0.5rem 1.5rem 0;
  }
}
.mc-drawer__content__title {
  font-size: var(--font-title-m, 1.5rem);
  margin: 0 0 0.5rem;
}
.mc-drawer__footer {
  display: flex;
  flex-direction: column;
  gap: 1rem;
  padding: 1rem 1.5rem;
  border-top: var(--border-width-s, 0.0625rem) solid var(--divider-color-primary, #cccccc);
}
@media (width >= 680px) {
  .mc-drawer__footer {
    justify-content: center;
    flex-direction: row;
  }
}
.mc-drawer-open {
  overflow: hidden;
}
.mc-drawer.is-open .mc-drawer__dialog {
  pointer-events: all;
  transform: translate3d(0, 0, 0);
  transition: visibility 0s linear 0s, transform 0.4s;
  visibility: visible;
}

.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;
}</style>
