import type { Meta, StoryObj } from '@storybook/react-vite'
import { expect } from 'storybook/test'

import { FlexibleFieldValue } from './flexible-field-value'
import { fieldDefinitions } from '../test/fixtures'

const def = (key: string) => fieldDefinitions.find((d) => d.key === key)!

const meta = {
  component: FlexibleFieldValue,
  tags: ['ai-generated'],
} satisfies Meta<typeof FlexibleFieldValue>

export default meta
type Story = StoryObj<typeof meta>

export const Term: Story = {
  args: { def: def('material'), value: 't-bronze', lang: 'en' },
  play: async ({ canvas }) => {
    await expect(await canvas.findByText('Bronze')).toBeVisible()
  },
}

export const Authority: Story = {
  args: { def: def('maker'), value: 'a-ada', lang: 'en' },
  play: async ({ canvas }) => {
    await expect(await canvas.findByText('Ada Lovelace')).toBeVisible()
  },
}

export const LocalizedText: Story = {
  args: {
    def: def('title_ml'),
    value: { sv: 'Brons-amfora', en: 'Bronze amphora' },
    lang: 'sv',
  },
  play: async ({ canvas }) => {
    await expect(canvas.getByText('Brons-amfora')).toBeVisible()
  },
}

export const Date: Story = {
  args: { def: def('made_on'), value: '2024-01-05', lang: 'en' },
  play: async ({ canvas }) => {
    await expect(canvas.queryByText('2024-01-05')).not.toBeInTheDocument()
    await expect(canvas.getByText(/Jan.*5.*2024/)).toBeVisible()
  },
}

export const Boolean: Story = {
  args: { def: def('is_fragment'), value: true, lang: 'en' },
  play: async ({ canvas }) => {
    await expect(canvas.getByText('Yes')).toBeVisible()
  },
}

export const UnknownRef: Story = {
  args: { def: def('material'), value: 't-missing', lang: 'en' },
  play: async ({ canvas }) => {
    await expect(await canvas.findByText(/t-missing\s*\(unknown\)/)).toBeVisible()
  },
}
