import * as React from 'react'
import type { StoryObj, Meta } from '@storybook/react-webpack5'
import { GridEditorInput } from '.'
import { Grid } from '../grids'
import { Calendar } from '@planview/pv-icons'
import { theme } from '@planview/pv-utilities'
import { GridCellDefault } from '../components'
import type { GridConfirmPayload } from '../types'
import type { InputType } from '@storybook/csf'

const hideControl: InputType = { control: false }

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

type InputRow = {
    id: string
    input: string
}

export const Default: StoryObj<typeof GridEditorInput> = {
    render: function Component(args) {
        const [rows, setRows] = React.useState<InputRow[]>([
            { id: '1', input: 'Sample data' },
            { id: '2', input: 'More data' },
            { id: '3', input: 'And another' },
        ])

        return (
            <Grid
                selectionMode="none"
                columns={[
                    {
                        id: 'input',
                        label: 'Input Editor',
                        sortable: false,
                        width: 300,
                        cell: {
                            editable: true,
                            Renderer(props) {
                                return (
                                    <GridCellDefault
                                        {...props}
                                        icon={args.icon}
                                    />
                                )
                            },
                            Editor(props) {
                                return <GridEditorInput {...args} {...props} />
                            },
                        },
                    },
                ]}
                rows={rows}
                onCellChange={(
                    confirm: GridConfirmPayload<InputRow, 'input'>
                ) => {
                    setRows((r) =>
                        r.map((row) => {
                            if (row.id === confirm.rowId) {
                                return {
                                    ...row,
                                    input: confirm.nextValue,
                                }
                            }
                            return row
                        })
                    )
                }}
            />
        )
    },
    args: {
        placeholder: 'This is a placeholder',
        icon: 'no icon' as any,
    },
    argTypes: {
        icon: {
            control: { type: 'radio' },
            options: ['no icon', 'with icon', 'with coloured icon'],
            mapping: {
                'no icon': null,
                'with icon': <Calendar color={theme.iconNormal} />,
                'with coloured icon': <Calendar color={theme.iconWarning} />,
            },
        },
    },
    parameters: {
        docs: {
            source: {
                code: `
const [rows, setRows] = React.useState<InputRow[]>([
    { id: '1', input: 'Sample data' },
    { id: '2', input: 'More data' },
    { id: '3', input: 'And another' },
])

return (
    <Grid
        selectionMode="none"
        columns={[
            {
                id: 'input',
                label: 'Input Editor',
                sortable: false,
                width: 300,
                cell: {
                    editable: true,
                    Renderer(props) {
                        return (
                            <GridCellDefault
                                {...props}
                                icon={args.icon}
                            />
                        )
                    },
                    Editor(props) {
                        return <GridEditorInput {...args} {...props} />
                    },
                },
            },
        ]}
        rows={rows}
        onCellChange={(
            confirm: GridConfirmPayload<InputRow, 'input'>
        ) => {
            setRows((r) =>
                r.map((row) => {
                    if (row.id === confirm.rowId) {
                        return {
                            ...row,
                            input: confirm.nextValue,
                        }
                    }
                    return row
                })
            )
        }}
    />
)
                `,
            },
        },
    },
}
