'use client'

import { type ReactNode, useMemo, useState } from 'react'

import { IDatasetFileInfo, IFileInfo } from '@/types'

import {
	CreateDatasetContext,
	type CreateDatasetContextProps
} from '@/contexts'

type CreateDatasetProviderProps = {
	children: ReactNode
}

export function CreateDatasetProvider({
	children
}: CreateDatasetProviderProps) {
	const [name, setName] = useState<string>('')
	//
	const [sourceType, setSourceType] = useState<string>('source')
	//
	const [fileInfo, setFileInfo] = useState<IFileInfo | null>(null)
	//
	const [datasetFileInfo, setDatasetFileInfo] = useState<IDatasetFileInfo>({
		path: '',
		separator: ';',
		encoding: 'utf-8'
	})
	//
	const [columns, setColumns] = useState<any[]>([])

	const contextValue = useMemo<CreateDatasetContextProps>(
		() => ({
			name,
			setName,
			//
			sourceType,
			setSourceType,
			//
			fileInfo,
			setFileInfo,
			//
			datasetFileInfo,
			setDatasetFileInfo,
			//
			columns,
			setColumns
		}),
		[
			name,
			setName,
			//
			sourceType,
			setSourceType,
			//
			fileInfo,
			setFileInfo,
			//
			datasetFileInfo,
			setDatasetFileInfo,
			//
			columns,
			setColumns
		]
	)

	return (
		<CreateDatasetContext.Provider value={contextValue}>
			{children}
		</CreateDatasetContext.Provider>
	)
}
