import {
  getArgs,
  getArgTypes,
  getSlotNames,
  getSlotsFragment,
  getDocsDescription,
} from "../../utils/storybook";

import UTabs from "../../ui.navigation-tabs/UTabs.vue";
import URow from "../../ui.container-row/URow.vue";
import ULabel from "../../ui.form-label/ULabel.vue";
import UTab from "../../ui.navigation-tab/UTab.vue";
import UBadge from "../../ui.text-badge/UBadge.vue";
import UAvatar from "../../ui.image-avatar/UAvatar.vue";
import UChip from "../../ui.other-chip/UChip.vue";
import UText from "../../ui.text-block/UText.vue";

import johnDoe from "../../ui.navigation-tab/storybook/assets/john-doe.png";

import type { Meta, StoryFn } from "@storybook/vue3-vite";
import type { Props } from "../types";

interface UTabsArgs extends Props {
  slotTemplate?: string;
  enum: "size";
}

export default {
  id: "8020",
  title: "Navigation / Tabs",
  component: UTabs,
  args: {
    modelValue: "1",
    options: [
      { label: "Dashboard", value: "1" },
      { label: "Orders", value: "2" },
      { label: "Settings", value: "3" },
    ],
  },
  argTypes: {
    ...getArgTypes(UTabs.__name),
  },
  parameters: {
    docs: {
      ...getDocsDescription(UTabs.__name),
    },
  },
} as Meta;

function getOptionsArray(count = 40) {
  return Array.from({ length: count }, (_, index) => ({
    label: `Tab ${index + 1}`,
    value: (index + 1).toString(),
  }));
}

const DefaultTemplate: StoryFn<UTabsArgs> = (args: UTabsArgs) => ({
  components: { UTabs },
  setup: () => ({ args, slots: getSlotNames(UTabs.__name) }),
  template: `
    <UTabs v-bind="args" v-model="args.modelValue">
      ${args.slotTemplate || getSlotsFragment("")}
    </UTabs>
  `,
});

const EnumTemplate: StoryFn<UTabsArgs> = (args: UTabsArgs, { argTypes }) => ({
  components: { UTabs, URow, ULabel },
  setup: () => ({ args, argTypes, getArgs }),
  template: `
    <UTabs
      v-for="option in argTypes?.[args.enum]?.options"
      v-bind="getArgs(args, option)"
      :key="option"
      v-model="args.modelValue"
      class="mb-4"
    />
  `,
});

export const Default = DefaultTemplate.bind({});
Default.args = {};

export const Sizes = EnumTemplate.bind({});
Sizes.args = { enum: "size" };

export const Scrollable = DefaultTemplate.bind({});
Scrollable.args = {
  options: getOptionsArray(),
  scrollable: true,
};

export const Block = DefaultTemplate.bind({});
Block.args = { block: true };

export const Square = DefaultTemplate.bind({});
Square.args = { square: true };
Square.parameters = {
  docs: {
    description: {
      story: "Set the same paddings for the tabs.",
    },
  },
};

export const DefaultSlot: StoryFn<UTabsArgs> = (args) => ({
  components: { UTabs, UTab },
  setup: () => ({ args, getOptionsArray }),
  template: `
    <UTabs v-model="args.modelValue">
      <UTab label="Custom Tab 1" value="1" />
      <UTab label="Custom Tab 2" value="2" disabled />
      <UTab label="Custom Tab 3" value="3" />
    </UTabs>
  `,
});

export const TabSlots: StoryFn<UTabsArgs> = (args) => ({
  components: { UTabs, UBadge, UAvatar, UChip, UText },
  setup: () => ({ args, johnDoe }),
  template: `
    <UTabs v-model="args.modelValue" v-bind="args">
      <template #left="{ index }">
        <UAvatar
          v-if="index === 0"
          :src="johnDoe"
          size="3xs"
          rounded="full"
        />
      </template>

      <template #label="{ label, index }">
        <UChip v-if="index === 1" size="sm">
          <UText :label="label" color="primary" class="mr-1.5" />
        </UChip>
      </template>

      <template #right="{ index }">
        <UBadge v-if="index === 2" label="New!" size="sm" />
      </template>
    </UTabs>
  `,
});

export const PrevNextSlots: StoryFn<UTabsArgs> = (args) => ({
  components: { UTabs },
  setup: () => ({ args, getOptionsArray }),
  template: `
    <UTabs
      v-bind="args"
      :options="getOptionsArray()"
      scrollable
      :config="{ next: 'flex items-center', prev: 'flex items-center' }"
    >
      <template #prev>
        <span class="cursor-pointer">◀️</span>
      </template>
      <template #next>
        <span class="cursor-pointer">▶️</span>
      </template>
    </UTabs>
  `,
});
PrevNextSlots.parameters = {
  docs: {
    description: {
      story:
        // eslint-disable-next-line vue/max-len
        "When the `scrollable` prop is set to `true`, you can replace the default `prev` and `next` slot icons with custom content.",
    },
  },
};
