<!--
 Licensed to the Apache Software Foundation (ASF) under one
 or more contributor license agreements.  See the NOTICE file
 distributed with this work for additional information
 regarding copyright ownership.  The ASF licenses this file
 to you under the Apache License, Version 2.0 (the
 "License"); you may not use this file except in compliance
 with the License.  You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing,
 software distributed under the License is distributed on an
 "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 KIND, either express or implied.  See the License for the
 specific language governing permissions and limitations
 under the License.
-->

<script lang="ts" module>
  type Ordering<T> = { key: keyof T; asc: boolean } | { key: undefined; asc: undefined };

  function orderData<T>(data: T[], key: keyof T, asc: boolean) {
    return data.sort((a, b) => {
      if (a[key] > b[key]) return asc ? 1 : -1;
      else if (a[key] < b[key]) return asc ? -1 : 1;
      return 0;
    });
  }
</script>

<script lang="ts">
  import { run } from 'svelte/legacy';

  import { asConst } from '$lib/utils/asConst';

  import Icon from './Icon.svelte';
  import { twMerge } from 'tailwind-merge';

  type T = $$Generic<{ id: string | number }>;
  type Column =
    | { label: string; slot?: undefined; key?: undefined; sortable?: false }
    | { slot: true; sortable?: undefined; key?: undefined; label?: undefined }
    | { label: string; slot?: false; key: keyof T; sortable: boolean };

  interface Props {
    data: Array<T>;
    columns: Array<Column>;
    emptyDataMessage: string;
    gridColsClass: string;
    header?: import('svelte').Snippet<[any]>;
    children?: import('svelte').Snippet<[any]>;
  }

  let { data, columns, emptyDataMessage, gridColsClass, header, children }: Props = $props();

  let ordering: Ordering<T> = $state({
    key: 'id',
    asc: true
  });

  let orderedData = $derived(ordering.key ? orderData(data, ordering.key, ordering.asc!) : data);

  let isBodyOverflowing = $state(false);
  let bodyElem: HTMLDivElement | null = $state(null);
  run(() => {
    if (bodyElem && bodyElem.scrollHeight > bodyElem.offsetHeight) {
      isBodyOverflowing = true;
    }
  });
</script>

<div class="overflow-auto flex-1">
  <!-- header -->
  <div
    class={twMerge(
      'flex h-[60px] min-h-[60px] bg-shade-l300 dark:bg-shade-d400 min-w-[1300px]',
      isBodyOverflowing && 'pr-5'
    )}
  >
    <div class="w-full grid {gridColsClass}">
      {#each columns as { label, slot, key, sortable }, idx (idx)}
        {#if slot}
          {@render header?.({ index: idx })}
        {:else}
          <button
            onclick={() => {
              if (!sortable || !key) return;
              ordering = {
                key: key,
                asc: ordering.key !== key ? true : !ordering.asc
              };
            }}
            class="flex items-center px-5 hover:cursor-pointer hover:bg-shade-l400 dark:hover:bg-shade-d200 dark:text-white justify-between outline-hidden focus:outline-hidden focus-visible:ring-3 ring-inset ring-blue-600/60 transition-colors"
          >
            <span>
              {label}
            </span>

            {#if sortable}
              <div class="flex flex-col">
                {#each asConst(['caretUp', 'caretDown']) as icon (icon)}
                  <Icon
                    name={icon}
                    class={twMerge(
                      'fill-shade-l800 stroke-shade-l800 dark:fill-shade-l900 dark:stroke-shade-l900 w-[18px] h-fit -mb-[4px] -my-[2px]',
                      ordering.key === key &&
                        ordering.asc &&
                        icon === 'caretUp' &&
                        ' stroke-black  fill-black dark:stroke-white dark:fill-white',
                      ordering.key === key &&
                        !ordering.asc &&
                        icon === 'caretDown' &&
                        ' stroke-black fill-black dark:stroke-white dark:fill-white'
                    )}
                  />
                {/each}
              </div>
            {/if}
          </button>
        {/if}
      {/each}
    </div>
  </div>

  <!-- body -->
  <div class="min-w-[1300px] h-[calc(100%-60px)] pb-3 overflow-auto" bind:this={bodyElem}>
    {#if data.length === 0}
      <div class="flex items-center justify-center text-gray-400 mt-14 text-lg">
        <em>{emptyDataMessage}</em>
      </div>
    {/if}

    {#each orderedData as item (item.id)}
      {@render children?.({
        item,
        baseClass: `grid ${gridColsClass} items-center h-[65px] border-b hoverable dark:text-white`
      })}
    {/each}
  </div>
</div>
