import { NumberInput } from '@based/ui'
import { useEffect, useState } from 'react'

export const meta: Based.UI.Extractor.MetaDescriptor = {
  name: 'NumberInput',
  group: 'Inputs',
  section: 'components',
  icon: 'edit',
  component: NumberInput,
  subtitle:
    'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.',
  showApi: true,
  showUsage: true,
}

export const blocks: Based.UI.Extractor.BlocksDescriptor[] = [
  {
    name: 'Default',
    type: 'component',
    description: 'Default NumberInput with lead icon and prefix.',
    style: {
      size: 'small',
      frame: false,
      flex: {
        justifyContent: 'center',
        alignItems: 'center',
      },
    },
    render: () => {
      const [value, setValue] = useState<number>(10)
      return (
        <div
          style={{
            width: 360,
          }}
        >
          <NumberInput
            value={value}
            onChange={setValue}
            placeholder="Add number"
          />
        </div>
      )
    },
  },
  {
    name: 'Changing outside value',
    type: 'component',
    description: 'NumberInput with value updated periodically from outside.',
    style: {
      size: 'small',
      frame: false,
      flex: {
        justifyContent: 'center',
        alignItems: 'center',
      },
    },
    render: () => {
      const [value, setValue] = useState<number>(10)

      useEffect(() => {
        const intervalId = setInterval(() => {
          setValue((v) => v + 5)
        }, 500)
        return () => clearInterval(intervalId)
      }, [])

      return (
        <div
          style={{
            width: 360,
          }}
        >
          <NumberInput
            value={value}
            onChange={setValue}
            placeholder="Add number"
          />
        </div>
      )
    },
  },
  {
    name: 'Disabled',
    type: 'component',
    description: 'Disabled NumberInput with lead icon and prefix.',
    style: {
      size: 'small',
      frame: false,
      flex: {
        justifyContent: 'center',
        alignItems: 'center',
      },
    },
    render: () => {
      const [value, setValue] = useState<number>()
      return (
        <div
          style={{
            width: 360,
          }}
        >
          <NumberInput value={value} onChange={setValue} disabled />
        </div>
      )
    },
  },
  {
    name: 'Error',
    type: 'component',
    description: 'NumberInput with error state.',
    style: {
      size: 'small',
      frame: false,
      flex: {
        justifyContent: 'center',
        alignItems: 'center',
      },
    },
    render: () => {
      const [value, setValue] = useState<number>()
      return (
        <div
          style={{
            width: 360,
          }}
        >
          <NumberInput
            value={value}
            onChange={setValue}
            placeholder="Something went wrong."
            error
          />
        </div>
      )
    },
  },
  {
    name: 'Suffix',
    type: 'component',
    description: 'NumberInput with a suffix.',
    style: {
      size: 'small',
      frame: false,
      flex: {
        justifyContent: 'center',
        alignItems: 'center',
      },
    },
    render: () => {
      const [value, setValue] = useState<number>(300)
      return (
        <div
          style={{
            width: 360,
          }}
        >
          <NumberInput value={value} onChange={setValue} suffix="USD" />
        </div>
      )
    },
  },
]
