import { describe, it, expect, beforeEach, afterEach } from 'vitest'
import { act, render } from '@testing-library/react'
import { Provider } from '../provider/widget-provider'
import { clearAllWidgetStores, getWidgetStore } from './widget-store-registry'
import { useTransformEnabled } from './use-transform-enabled'

beforeEach(() => clearAllWidgetStores())
afterEach(() => clearAllWidgetStores())

function Probe({
  id,
  transformId,
  onValue,
}: {
  id: string
  transformId: string
  onValue: (v: boolean) => void
}) {
  const enabled = useTransformEnabled(id, transformId)
  onValue(enabled)
  return null
}

describe('useTransformEnabled', () => {
  it('returns false when the transform has not been registered', () => {
    const calls: boolean[] = []
    render(
      <Provider id='ute-1' data={null}>
        <Probe
          id='ute-1'
          transformId='searcher'
          onValue={(v) => calls.push(v)}
        />
      </Provider>,
    )
    expect(calls[calls.length - 1]).toBe(false)
  })

  it('returns true when `transformStates.<id>.enabled` is true; updates reactively', () => {
    const calls: boolean[] = []
    render(
      <Provider id='ute-2' data={null}>
        <Probe
          id='ute-2'
          transformId='searcher'
          onValue={(v) => calls.push(v)}
        />
      </Provider>,
    )
    // Initial — no transform registered.
    expect(calls[calls.length - 1]).toBe(false)

    // Simulate SearcherToggle enabling itself.
    act(() => {
      const store = getWidgetStore('ute-2')
      store.setState({
        transformStates: {
          ...store.getState().transformStates,
          searcher: { enabled: true },
        },
      })
    })
    expect(calls[calls.length - 1]).toBe(true)

    // Disable.
    act(() => {
      const store = getWidgetStore('ute-2')
      store.setState({
        transformStates: {
          ...store.getState().transformStates,
          searcher: { enabled: false },
        },
      })
    })
    expect(calls[calls.length - 1]).toBe(false)
  })

  it('returns false when `enabled` is a non-true truthy value (strict boolean check)', () => {
    // Defensive: the helper uses `=== true`, so non-boolean truthy values
    // shouldn't accidentally activate features. Guards against future
    // store-shape drift.
    const calls: boolean[] = []
    render(
      <Provider id='ute-3' data={null}>
        <Probe
          id='ute-3'
          transformId='searcher'
          onValue={(v) => calls.push(v)}
        />
      </Provider>,
    )
    act(() => {
      const store = getWidgetStore('ute-3')
      store.setState({
        transformStates: {
          ...store.getState().transformStates,
          // @ts-expect-error — intentional shape violation for the boundary test.
          searcher: { enabled: 'yes' },
        },
      })
    })
    expect(calls[calls.length - 1]).toBe(false)
  })

  it('returns false for an unknown transform id while others are enabled', () => {
    const calls: boolean[] = []
    render(
      <Provider id='ute-4' data={null}>
        <Probe
          id='ute-4'
          transformId='not-a-real-transform'
          onValue={(v) => calls.push(v)}
        />
      </Provider>,
    )
    act(() => {
      const store = getWidgetStore('ute-4')
      store.setState({
        transformStates: {
          ...store.getState().transformStates,
          searcher: { enabled: true },
        },
      })
    })
    expect(calls[calls.length - 1]).toBe(false)
  })
})
