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

const hideControl: InputType = { control: false }

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

const options: ComboboxTreeOption[] = [
    {
        label: 'Team A',
        value: 'avatar-1',
        avatar: 'img/avatars/400-8.jpg',
        children: [
            {
                label: 'Alice',
                value: 'avatar-1-1',
                avatar: 'img/avatars/400-8.jpg',
            },
            {
                label: 'Bob',
                value: 'avatar-1-2',
                avatar: 'img/avatars/400-8.jpg',
            },
        ],
    },
    {
        label: 'Team B',
        value: 'avatar-2',
        avatar: 'img/avatars/400-9.jpg',
        children: [
            {
                label: 'Charlie',
                value: 'avatar-2-1',
                avatar: 'img/avatars/400-9.jpg',
            },
            {
                label: 'David',
                value: 'avatar-2-2',
                avatar: 'img/avatars/400-9.jpg',
            },
        ],
    },
]

type UserRow = {
    id: string
    user: ComboboxTreeOption[]
}

function UserComboboxEditor(
    props: CellEditorParams<UserRow>
): React.JSX.Element {
    return (
        <GridEditorComboboxTreeMulti
            {...props}
            value={props.value}
            options={options}
        />
    )
}

export const Default: StoryObj<typeof GridEditorComboboxTreeMulti<UserRow>> = {
    render: function Component(args) {
        const [rows, setRows] = React.useState<UserRow[]>([
            {
                id: '1',
                user: [
                    {
                        label: 'Bob',
                        value: 'avatar-1-2',
                        avatar: 'img/avatars/400-8.jpg',
                    },
                ],
            },
            { id: '2', user: [] },
            {
                id: '3',
                user: [
                    {
                        label: 'Charlie',
                        value: 'avatar-2-1',
                        avatar: 'img/avatars/400-9.jpg',
                    },
                    {
                        label: 'David',
                        value: 'avatar-2-2',
                        avatar: 'img/avatars/400-9.jpg',
                    },
                ],
            },
        ])

        /* This is a storybook concern so its intentionally left out of code examples */
        const MappedComponent = React.useCallback(
            (props: CellEditorParams<UserRow>) => (
                <UserComboboxEditor {...props} {...args} />
            ),
            [args]
        )

        return (
            <Grid
                selectionMode="none"
                columns={[
                    {
                        id: 'user',
                        label: 'Combobox Tree Multi Editor',
                        sortable: false,
                        width: 300,
                        cell: {
                            editable: true,
                            Renderer(props) {
                                return <GridCellChips {...props} />
                            },
                            Editor: MappedComponent,
                        },
                    },
                ]}
                rows={rows}
                onCellChange={(
                    confirm: GridConfirmPayload<
                        UserRow,
                        'user',
                        ComboboxTreeOption[]
                    >
                ) => {
                    setRows((r) =>
                        r.map((row) => {
                            if (row.id === confirm.rowId) {
                                return {
                                    ...row,
                                    user: confirm.nextValue,
                                }
                            }
                            return row
                        })
                    )
                }}
            />
        )
    },
}
