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

const hideControl: InputType = { control: false }

export default {
    title: 'pv-grid/Components/Editors/GridEditorDatePicker',
    component: GridEditorDatePicker,
    parameters: {
        controls: {
            exclude: ['label', 'mode', 'rowId', 'columnId'],
        },
    },
    argTypes: {
        icon: {
            options: ['no icon', 'with custom icon'],
            mapping: {
                'no icon': undefined,
                'with custom icon': <PlusCircle />,
            },
            control: {
                type: 'radio',
            },
        },
        columnId: hideControl,
        rowId: hideControl,
        tabIndex: hideControl,
        value: hideControl,
    },
} satisfies Meta<typeof GridEditorDatePicker>

export const Default: StoryObj<typeof GridEditorDatePicker> = {
    render: function Component(args) {
        const [rows, setRows] = React.useState([
            { id: '1', date: '2023-03-23' },
            { id: '2', date: '2023-06-07' },
            { id: '3', date: '2023-06-07' },
        ])

        return (
            <Grid
                selectionMode="none"
                columns={[
                    {
                        id: 'date',
                        label: 'DatePicker Editor',
                        sortable: false,
                        movable: false,
                        width: 300,
                        cell: {
                            editable: true,
                            Renderer(props) {
                                return (
                                    <GridCellDefault
                                        icon={args.icon as React.JSX.Element}
                                        {...props}
                                    />
                                )
                            },
                            Editor(props) {
                                return (
                                    <GridEditorDatePicker
                                        {...args}
                                        {...props}
                                    />
                                )
                            },
                        },
                    },
                ]}
                rows={rows}
                onCellChange={(
                    confirm: GridConfirmPayload<{ id: string }, 'date', string>
                ) => {
                    setRows((r) =>
                        r.map((row) => {
                            if (row.id === confirm.rowId) {
                                return {
                                    ...row,
                                    date: confirm.nextValue,
                                }
                            }
                            return row
                        })
                    )
                }}
            />
        )
    },
}

Default.parameters = {
    docs: {
        source: {
            code: `
const [rows, setRows] = React.useState([
    { id: '1', date: '2023-03-23' },
    { id: '2', date: '2023-06-07' },
    { id: '3', date: '2023-06-07' },
])

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