<svelte:options customElement={{ tag: 'm-file-uploader-item' }} />

<script lang="ts">
  import Button from '../button/Button.svelte';
  import Divider from '../divider/Divider.svelte';
  import {
    CheckCircle32,
    Cross20,
    CrossCircleFilled20,
    WarningCircle32,
  } from '@mozaic-ds/icons-svelte';

  export type NormalizedFile = {
    name: string;
    size?: number;
    type?: HTMLInputElement['accept'];
  };

  interface Props {
    /**
     * Represents the file to be displayed in the file uploader item.
     */
    file: NormalizedFile;
    /**
     * Layout format of the item.
     */
    format?: 'inline' | 'stacked';
    /**
     * Indicates if the file is valid according to the uploader's validation rules.
     */
    valid?: boolean;
    /**
     * Custom error message for the file.
     */
    errormessage?: string;
    /**
     * Label for the delete button.
     */
    deletebuttonlabel?: string;
    /**
     * Optional informational text displayed under the file name.
     */
    information?: string;
    /**
     * Callback to call when delete button is clicked.
     */
    ondelete: () => void;
  }

  let {
    file,
    format = 'inline',
    valid,
    errormessage,
    deletebuttonlabel = 'Delete',
    information,
    ondelete,
  }: Props = $props();

  const isStacked = $derived(format === 'stacked');

  const stateModifier = $derived(valid ? 'default' : 'error');

  const stateIconMap = {
    default: CheckCircle32,
    error: WarningCircle32,
  };

  const StateIcon = $derived(stateIconMap[stateModifier]);
</script>

<div
  class={{
    'mc-file-uploader-item': true,
    [`mc-file-uploader-item--${stateModifier}`]: stateModifier !== 'default',
  }}
>
  <div
    class={{
      'mc-file-uploader-item__container': true,
      [`mc-file-uploader-item__container--${format}`]: true,
    }}
  >
    <div
      class={{
        'mc-file-uploader-item__meta-row': true,
        'mc-file-uploader-item__meta-row--with-info': information,
      }}
    >
      <span class="mc-file-uploader-item__status-icon">
        <StateIcon />
      </span>

      <div class="mc-file-uploader-item__info-content">
        <span class="mc-file-uploader-item__label">
          <slot name="name" {file}>
            {file.name}
          </slot>
        </span>

        {#if information || $$slots.information}
          <span class="mc-file-uploader-item__details">
            <slot name="information" {file}>
              {information}
            </slot>
          </span>
        {/if}
      </div>
    </div>

    {#if isStacked}
      <Divider />
      <div class="mc-file-uploader-item__actions-container">
        <Button ghost size="s" iconposition="left" aria-label="Delete file" onclick={ondelete}>
          {deletebuttonlabel}

          <Cross20 slot="icon" />
        </Button>
      </div>
    {:else}
      <div class="mc-file-uploader-item__delete-button-container">
        <Button ghost size="s" iconposition="only" aria-label="Delete file" onclick={ondelete}>
          {deletebuttonlabel}

          <CrossCircleFilled20 slot="icon" />
        </Button>
      </div>
    {/if}
  </div>

  {#if !valid && (errormessage || $$slots.errorMessage)}
    <span class="mc-file-uploader-item__error-message">
      <slot name="errorMessage">
        {errormessage}
      </slot>
    </span>
  {/if}
</div>

<style>/**
 * Do not edit directly, this file was auto-generated.
 */
.mc-file-uploader {
  display: flex;
  flex-direction: column;
  gap: 1rem;
  box-sizing: border-box;
}
.mc-file-uploader__files-list {
  display: flex;
  flex-direction: column;
  gap: 1rem;
}
.mc-file-uploader__hidden-input {
  clip-path: inset(100%);
  height: 1px;
  overflow: hidden;
  position: absolute;
  white-space: nowrap;
  padding: 0;
  width: 1px;
}
.mc-file-uploader__input {
  display: inline-flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 0.5rem;
  line-height: var(--line-height-s, 1.3);
  font-weight: var(--font-weight-semi-bold, 600);
  border-radius: var(--border-radius-m, 0.5rem);
  color: var(--file-uploader-color-drag-and-drop-text-default, #242938);
}
.mc-file-uploader__input .mc-file-uploader__button {
  width: 100%;
}
.mc-file-uploader__input--disabled .mc-file-uploader__button.mc-button.mc-button--outlined {
  background-color: var(--button-state-disabled-background, #d9d9d9);
  border-color: transparent;
  color: var(--button-state-disabled-color, #737373);
  cursor: not-allowed;
}
.mc-file-uploader__input-title {
  height: 1.125rem;
  font-size: var(--font-size-150, 1rem);
}
.mc-file-uploader__input-subtitle {
  height: 1.125rem;
  font-size: var(--font-size-100, 0.875rem);
}
.mc-file-uploader--draggable .mc-file-uploader__input {
  padding: 2rem;
  outline: 0.125rem dashed var(--file-uploader-color-drag-and-drop-border-default, #464e63);
  display: flex;
}
.mc-file-uploader--draggable .mc-file-uploader__input--disabled {
  outline-color: var(--file-uploader-color-drag-and-drop-border-disabled, #b3b3b3);
  color: var(--file-uploader-color-drag-and-drop-text-disabled, #b3b3b3);
  cursor: not-allowed;
}
.mc-file-uploader--draggable .mc-file-uploader__input--dragged {
  outline-width: 0.25rem;
}

.mc-file-uploader-item {
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
}
.mc-file-uploader-item__container {
  border: 1px solid var(--file-uploader-color-file-item-border-default, #cccccc);
  border-radius: var(--border-radius-m, 0.5rem);
  padding: 0.75rem;
}
.mc-file-uploader-item__meta-row {
  display: flex;
  align-items: center;
  flex-grow: 1;
}
.mc-file-uploader-item__meta-row--with-info {
  align-items: start;
}
.mc-file-uploader-item__status-icon {
  fill: var(--file-uploader-color-file-item-icon-valid, #3f9e10);
}
.mc-file-uploader-item__info-content {
  display: flex;
  flex-direction: column;
  flex-grow: 1;
  padding-left: 0.5rem;
  padding-right: 1rem;
}
.mc-file-uploader-item__label {
  color: var(--file-uploader-color-file-item-text-file-name, #000000);
  font-size: var(--font-size-100, 0.875rem);
  line-height: var(--line-height-s, 1.3);
  font-weight: var(--font-weight-semi-bold, 600);
}
.mc-file-uploader-item__details {
  color: var(--file-uploader-color-file-item-text-information, #666666);
  font-size: var(--font-size-50, 0.75rem);
  line-height: var(--line-height-s, 1.3);
  font-weight: var(--font-weight-regular, 400);
}
.mc-file-uploader-item__error-message {
  color: var(--field-color-validation-invalid, #c61112);
  font-size: var(--font-size-100, 0.875rem);
  line-height: var(--line-height-s, 1.3);
  font-weight: var(--font-weight-regular, 400);
}
.mc-file-uploader-item__container--inline {
  display: flex;
  align-items: center;
}
.mc-file-uploader-item__container--stacked {
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
}
.mc-file-uploader-item__delete-button-container {
  padding-left: 0.25rem;
  margin-left: 0.25rem;
  border-left: 1px solid var(--divider-color-primary, #cccccc);
}
.mc-file-uploader-item__actions-container {
  padding: 0 2rem;
  display: flex;
  align-items: center;
  gap: 0.25rem;
  justify-content: center;
}
.mc-file-uploader-item__action {
  padding: 0 0.75rem;
  flex-grow: 1;
}
.mc-file-uploader-item--error .mc-file-uploader-item__container {
  border-width: var(--border-width-m, 0.125rem);
  border-color: var(--file-uploader-color-file-item-border-invalid, #ea302d);
}
.mc-file-uploader-item--error .mc-file-uploader-item__status-icon {
  fill: var(--file-uploader-color-file-item-icon-invalid, #ea302d);
}
.mc-file-uploader-item--loading .mc-file-uploader-item__container {
  gap: 1rem;
  flex-direction: column;
  align-items: normal;
}
.mc-file-uploader-item--loading .mc-file-uploader-item__status-icon {
  fill: var(--color-standalone-disabled, #b3b3b3);
}
.mc-file-uploader-item--loading .mc-file-uploader-item__label,
.mc-file-uploader-item--loading .mc-file-uploader-item__details {
  color: var(--color-standalone-disabled, #b3b3b3);
}</style>
