<svelte:options
  customElement={{
    tag: 'm-tag',
    props: {
      checked: { reflect: true, type: 'Boolean', attribute: 'checked' },
      disabled: { reflect: true, type: 'Boolean', attribute: 'disabled' },
    },
  }}
/>

<script lang="ts">
  import { CrossCircleFilled24, CrossCircleFilled20 } from '@mozaic-ds/icons-svelte';
  import NumberBadge from '../numberbadge/NumberBadge.svelte';
  /**
   * A Tag is a UI element used to filter data, categorize, select or deselect an option. It can appear standalone, in a group, or embedded within other components. Depending on its use, a tag can be interactive (clickable, removable, selectable) or static (serving as a visual indicator).
   *
   * @event remove-tag {CustomEvent<string>} - Emits when the remove button of a tag is clicked, passing the tag's ID.
   * @slot icon - Use this slot to insert an icon in the tag.
   */
  interface Props {
    [key: string]: any;
    /**
     * Defines the behavior and layout of the tag.
     */
    type?: 'informative' | 'interactive' | 'contextualised' | 'removable' | 'selectable';
    /**
     * Determines the size of the tag.
     */
    size?: 's' | 'm' | 'l';
    /**
     * A unique identifier for the tag, used to associate the label with the form element. **Required** when type is 'selectable' or 'removable'.
     */
    id?: string;
    /**
     * The name attribute for the tag element, typically used for form submission. (only relevant for type: 'selectable').
     */
    name?: string;
    /**
     * The text label displayed next to the tag.
     */
    label: string;
    /**
     * The tag's checked state. Used only for type: 'selectable'.
     */
    checked?: boolean;
    /**
     * If `true`, disables the tag, making it non-interactive. Applicable to selectable, interactive, and contextualised types.
     */
    disabled?: boolean;
    /**
     * A number displayed in the badge when the tag is contextualised.
     */
    contextualisednumber?: number;
    /**
     * Accessible label text for the remove button in removable tags.
     */
    removablelabel?: string;
    /**
     * Callback to trigger when the remove button is clicked.
     */
    onremove?: () => void;
  }

  let {
    type = 'informative',
    size = 'm',
    id,
    name,
    label,
    checked = $bindable(),
    disabled,
    contextualisednumber = 99,
    removablelabel,
    onremove,
    class: className,
    ...attrs
  }: Props = $props();
  let element = $state<HTMLElement | null>(null);

  const onRemove = () => {
    onremove?.();

    const event = new CustomEvent('remove-tag', {
      detail: id,
      bubbles: true,
      composed: true,
    });
    element?.dispatchEvent(event);
  };
</script>

{#if type === 'selectable'}
  <label for={id} class={['mc-tag', `mc-tag-${type}`, `mc-tag--${size}`, className]} {...attrs}>
    {#if !checked}
      <slot name="icon" />
    {/if}
    <input type="checkbox" class="mc-tag__input" {id} {name} {disabled} bind:checked />
    <span class="mc-tag__label">{label}</span>
  </label>
{:else if type === 'interactive'}
  <button
    class={['mc-tag', `mc-tag-${type}`, `mc-tag--${size}`, className]}
    type="button"
    {disabled}
    {...attrs}
  >
    <slot name="icon" />
    <span class="mc-tag__label">{label}</span>
  </button>
{:else if type === 'contextualised'}
  <button
    class={['mc-tag', `mc-tag-${type}`, `mc-tag--${size}`, className]}
    type="button"
    {disabled}
    {...attrs}
  >
    <NumberBadge
      appearance="inverse"
      label={contextualisednumber}
      size={size === 'l' ? 'm' : undefined}
    />
    <span class="mc-tag__label">{label}</span>
  </button>
{:else if type === 'removable'}
  <span
    class={['mc-tag', `mc-tag-${type}`, `mc-tag--${size}`, className]}
    bind:this={element}
    {...attrs}
  >
    <span class="mc-tag__label">{label}</span>
    <button class="mc-tag-removable__remove" type="button" onclick={onRemove}>
      <span class="mc-tag-removable__icon">
        {#if size === 's'}
          <CrossCircleFilled20 />
        {:else}
          <CrossCircleFilled24 />
        {/if}
      </span>
      <span class="mc-tag-removable__text">{removablelabel}</span>
    </button>
  </span>
{:else}
  <span class={['mc-tag', `mc-tag-${type}`, `mc-tag--${size}`, className]}>
    <span class="mc-tag__label">{label}</span>
  </span>
{/if}

<style>/**
 * Do not edit directly, this file was auto-generated.
 */
.mc-tag {
  font-size: var(--font-size-100, 0.875rem);
  border-radius: 2rem;
  height: 2rem;
  padding-inline: 0.75rem;
  box-sizing: border-box;
  display: inline-flex;
  align-items: center;
  background: var(--tag-color-background-standard, #ffffff);
  color: var(--tag-color-text-standard, #242938);
  border: var(--border-width-s, 0.0625rem) solid var(--tag-color-border-informative, #c9d0de);
}
.mc-tag--s {
  font-size: var(--font-size-50, 0.75rem);
  border-radius: 1.5rem;
  height: 1.5rem;
  padding-inline: 0.5rem;
}
.mc-tag--l {
  font-size: var(--font-size-100, 0.875rem);
  border-radius: 2.5rem;
  height: 2.5rem;
  padding-inline: 1rem;
}
.mc-tag.is-disabled, .mc-tag:disabled {
  cursor: not-allowed;
  background: var(--color-background-disabled, #d9d9d9);
  color: var(--color-text-disabled, #737373);
  border-color: var(--color-background-disabled, #d9d9d9);
  box-shadow: none;
}
.mc-tag.is-disabled .mc-badge, .mc-tag:disabled .mc-badge {
  color: currentcolor;
}

.mc-tag-interactive {
  text-decoration: none;
  outline: none;
  cursor: pointer;
  font-weight: var(--font-weight-semi-bold, 600);
}
.mc-tag-interactive:not(:disabled):hover {
  box-shadow: inset 0 0 0 var(--border-width-s, 0.0625rem) var(--tag-color-border-interactive, #8891aa);
}
.mc-tag-interactive: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-tag-contextualised {
  gap: 0.25rem;
  text-decoration: none;
  outline: none;
  color: var(--tag-color-text-inverse, #ffffff);
  border: none;
  background: var(--tag-color-background-selected, #117f03);
  cursor: pointer;
  font-weight: var(--font-weight-semi-bold, 600);
  padding-inline-end: 1rem;
  padding-inline-start: 0.5rem;
}
.mc-tag-contextualised:not(:disabled):hover {
  background: var(--tag-color-background-selected-hover, #006902);
}
.mc-tag-contextualised: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-tag-contextualised.mc-tag--s {
  padding-inline-end: 0.5rem;
  padding-inline-start: 0.25rem;
}

.mc-tag-removable {
  padding: 0 0 0 1rem;
  background: var(--tag-color-background-inverse, #242938);
  color: var(--tag-color-text-inverse, #ffffff);
  border: none;
}
.mc-tag-removable__remove {
  width: 2rem;
  height: 2rem;
  background-size: 1.5rem;
  background: transparent;
  border: none;
  margin: 0 0 0 0.125rem;
  padding: 0.25rem;
  display: block;
  cursor: pointer;
  border-radius: var(--border-radius-full, 100%);
}
.mc-tag-removable__remove:hover {
  background: var(--tag-color-background-inverse-hover, #343b4c);
}
.mc-tag-removable__remove: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-tag-removable__icon {
  fill: var(--tag-color-delete-icon, #c9d0de);
  width: 1.5rem;
  height: 1.5rem;
}
.mc-tag-removable__text {
  clip-path: inset(100%);
  height: 1px;
  overflow: hidden;
  position: absolute;
  white-space: nowrap;
  padding: 0;
  width: 1px;
}
.mc-tag-removable.mc-tag--s {
  padding: 0 0 0 0.5rem;
}
.mc-tag-removable.mc-tag--s .mc-tag-removable__remove {
  width: 1.5rem;
  height: 1.5rem;
  padding: 0.125rem;
}
.mc-tag-removable.mc-tag--s .mc-tag-removable__icon {
  width: 1.25rem;
  height: 1.25rem;
}
.mc-tag-removable.mc-tag--l .mc-tag-removable__remove {
  width: 2.5rem;
  height: 2.5rem;
}
.mc-tag-removable.mc-tag--l .mc-tag-removable__icon {
  width: 2rem;
  height: 2rem;
}

.mc-tag-selectable {
  gap: 0.25rem;
  white-space: nowrap;
  cursor: pointer;
  padding-inline-start: 0.5rem;
  font-weight: var(--font-weight-semi-bold, 600);
}
.mc-tag-selectable:hover {
  box-shadow: inset 0 0 0 var(--border-width-s, 0.0625rem) var(--tag-color-border-interactive, #8891aa);
}
.mc-tag-selectable:has(: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-tag-selectable:has(:checked) {
  background: var(--tag-color-background-selected, #117f03);
  color: var(--tag-color-text-inverse, #ffffff);
  border: none;
}
.mc-tag-selectable:has(:checked)::before {
  display: inline-block;
  content: "";
  height: 1.25rem;
  width: 1.25rem;
  background-color: var(--tag-color-text-inverse, #ffffff);
  mask: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' aria-hidden='true' height='1.25rem' width='1.25rem' viewBox='0 0 20 20'%3E%3Cg clip-path='url(%23a)'%3E%3Cpath fill-rule='evenodd' d='M14.697 6.553a.75.75 0 0 1 0 1.06l-5.833 5.834a.75.75 0 0 1-1.06 0l-2.5-2.5a.75.75 0 0 1 1.06-1.06l1.97 1.97 5.303-5.304a.75.75 0 0 1 1.06 0'/%3E%3C/g%3E%3Cdefs%3E%3CclipPath id='a'%3E%3Cpath fill='%23fff' d='M0 0h20v20H0z'/%3E%3C/clipPath%3E%3C/defs%3E%3C/svg%3E") no-repeat center/contain;
}
.mc-tag-selectable:has(:checked):not(:has(:disabled)):hover {
  background: var(--tag-color-background-selected-hover, #006902);
  border-color: var(--tag-color-background-selected-hover, #006902);
  box-shadow: none;
}
.mc-tag-selectable:has(:disabled) {
  cursor: not-allowed;
  background: var(--color-background-disabled, #d9d9d9);
  color: var(--color-text-disabled, #737373);
  border-color: var(--color-background-disabled, #d9d9d9);
  box-shadow: none;
}
.mc-tag-selectable:has(:disabled)::before {
  mask: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' aria-hidden='true' height='1.25rem' width='1.25rem' viewBox='0 0 20 20'%3E%3Cg clip-path='url(%23a)'%3E%3Cpath fill-rule='evenodd' d='M14.697 6.553a.75.75 0 0 1 0 1.06l-5.833 5.834a.75.75 0 0 1-1.06 0l-2.5-2.5a.75.75 0 0 1 1.06-1.06l1.97 1.97 5.303-5.304a.75.75 0 0 1 1.06 0'/%3E%3C/g%3E%3Cdefs%3E%3CclipPath id='a'%3E%3Cpath fill='%23fff' d='M0 0h20v20H0z'/%3E%3C/clipPath%3E%3C/defs%3E%3C/svg%3E") no-repeat center/contain;
}
.mc-tag-selectable .mc-tag__input {
  clip-path: inset(100%);
  height: 1px;
  overflow: hidden;
  position: absolute;
  white-space: nowrap;
  padding: 0;
  width: 1px;
}
.mc-tag-selectable .mc-tag__label {
  user-select: none;
}
.mc-tag-selectable.mc-tag--s {
  padding-inline-start: 0.25rem;
  gap: 0.125rem;
}
.mc-tag-selectable.mc-tag--l {
  padding-inline-start: 0.75rem;
}

.mc-tag-interactive {
  gap: 0.25rem;
}</style>
