import React from 'react'
import type { GridRowData, GridRowMeta } from '../../types'

import type { GridApi, SpreadsheetGridApi } from '../../api'
import type { SpreadsheetGridProps } from './types'
import { Grid } from '../grid'
import { GridPrivateSettingsProvider } from '../../context/grid-private-settings-context'

const SpreadsheetGridImpl = <
    TDataModel extends GridRowData,
    TMetaModel extends GridRowMeta<TDataModel>,
>(
    {
        onRangeSelectionChange,
        rangeSelection,
        onRangeCopy,
        onBulkCellChange,
        ...props
    }: SpreadsheetGridProps<TDataModel>,
    ref: React.ForwardedRef<GridApi>
) => (
    <GridPrivateSettingsProvider
        spreadsheet
        rangeSelection={rangeSelection}
        onRangeSelectionChange={onRangeSelectionChange}
        onRangeCopy={onRangeCopy}
        onBulkCellChange={onBulkCellChange}
    >
        <Grid<TDataModel, TMetaModel>
            ref={ref}
            {...props}
            selectionMode="none"
        />
    </GridPrivateSettingsProvider>
)
/**
 * The `SpreadsheetGrid` component is a specialized, **experimental** version of the `Grid` component
 * that provides spreadsheet-like functionality, including range selection.
 *
 * It is identical to the `Grid` component in most respects with these exceptions:
 *
 *  - Row selection is not available in this mode (`selectionMode` is forced to `"none"` internally).
 *  - Editing requires double-clicking on an editable cell
 *
 * **Important:** This component is currently **experimental**, and will be subject to breaking changes in future releases.
 * Feel free to try it out and provide feedback to the Design System Team, but avoid using it in production applications at this time.
 */
export const SpreadsheetGrid = React.forwardRef(SpreadsheetGridImpl) as <
    TDataModel extends GridRowData,
    TMetaModel extends GridRowMeta<TDataModel>,
>(
    props: SpreadsheetGridProps<TDataModel, TMetaModel> & {
        ref?: React.ForwardedRef<SpreadsheetGridApi>
    }
) => ReturnType<typeof SpreadsheetGridImpl>
