import * as React from 'react'
import type { StoryObj, StoryFn, Meta } from '@storybook/react-webpack5'
import { GridEditorRadio } from '.'
import { Grid } from '../grids'
import type { Column, GridConfirmPayload } from '../types'
import type { InputType } from '@storybook/csf'

const hideControl: InputType = { control: false }

export default {
    title: 'pv-grid/Components/Editors/GridEditorRadio',
    component: GridEditorRadio,
    parameters: {
        controls: {
            exclude: ['label', 'mode', 'rowId'],
        },
    },
    argTypes: {
        columnId: hideControl,
        rowId: hideControl,
        tabIndex: hideControl,
        value: hideControl,
    },
} satisfies Meta<typeof GridEditorRadio>

type Project = {
    id: string
    title: string
    isDefault: boolean
}

export const Default: StoryObj<typeof GridEditorRadio> = {
    render: function Component() {
        const [rows, setRows] = React.useState<Project[]>([
            { id: '1', title: 'Alpha Project', isDefault: true },
            { id: '2', title: 'Beta Project', isDefault: false },
            { id: '3', title: 'Gamma Project', isDefault: false },
        ])

        return (
            <Grid
                selectionMode="none"
                columns={[
                    {
                        id: 'title',
                        label: 'Project',
                        width: 300,
                        cell: {
                            editable: true,
                        },
                    },
                    {
                        id: 'isDefault',
                        label: 'Default',
                        width: 80,
                        cell: {
                            editable: true,
                            hybridEditor: true,
                            Editor: GridEditorRadio,
                        },
                    },
                ]}
                rows={rows}
                onCellChange={(
                    confirm: GridConfirmPayload<Project, keyof Project>
                ) => {
                    setRows((r) =>
                        r.map((row) => ({
                            ...row,
                            isDefault:
                                confirm.columnId === 'isDefault'
                                    ? row.id === confirm.rowId
                                    : row.isDefault,
                        }))
                    )
                }}
            />
        )
    },
    parameters: {
        docs: {
            source: {
                code: `
const [rows, setRows] = React.useState<Project[]>([
    { id: '1', title: 'Alpha Project', isDefault: true },
    { id: '2', title: 'Beta Project', isDefault: false },
    { id: '3', title: 'Gamma Project', isDefault: false },
])

return (
    <Grid
        selectionMode="none"
        columns={[
            {
                id: 'title',
                label: 'Project',
                width: 300,
                cell: {
                    editable: true,
                },
            },
            {
                id: 'isDefault',
                label: 'Default',
                width: 80,
                cell: {
                    editable: true,
                    hybridEditor: true,
                    Editor: GridEditorRadio,
                },
            },
        ]}
        rows={rows}
        onCellChange={(confirm: GridConfirmPayload<Project, keyof Project>) => {
            setRows((r) =>
                r.map((row) => ({
                    ...row,
                    isDefault:
                        confirm.columnId === 'isDefault'
                            ? row.id === confirm.rowId
                            : row.isDefault,
                }))
            )
        }}
    />
)
                `,
            },
        },
    },
}

export const Disabled: StoryFn = () => {
    const [rows, setRows] = React.useState<Project[]>([
        { id: '1', title: 'Alpha Project', isDefault: true },
        { id: '2', title: 'Beta Project', isDefault: false },
        { id: '3', title: 'Gamma Project', isDefault: false },
    ])

    const columns: Column<Project>[] = React.useMemo(
        () => [
            {
                id: 'title',
                label: 'Project',
                width: 300,
                cell: {
                    editable: true,
                },
            },
            {
                id: 'isDefault',
                label: 'Default',
                width: 80,
                cell: {
                    editable: ({ row }) => row.id !== '2',
                    hybridEditor: 'always',
                    Editor: GridEditorRadio,
                },
            },
        ],
        []
    )

    return (
        <Grid
            selectionMode="none"
            columns={columns}
            rows={rows}
            onCellChange={(
                confirm: GridConfirmPayload<Project, keyof Project>
            ) => {
                setRows((r) =>
                    r.map((row) => ({
                        ...row,
                        isDefault:
                            confirm.columnId === 'isDefault'
                                ? row.id === confirm.rowId
                                : row.isDefault,
                    }))
                )
            }}
        />
    )
}
