import type { BglFormSchemaT } from '@bagelink/vue'

import type { MaybeRefOrGetter } from 'vue'
import { getFallbackSchema } from '@bagelink/vue'
import { computed, toValue } from 'vue'

export { useAddToCalendar } from './useAddToCalendar'
export { useDevice } from './useDevice'
export { useExcel } from './useExcel'
export { useLocalStore } from './useLocalStore'
export { usePolling } from './usePolling'
export { useQuery } from './useQuery'
export { useResizableLayoutProvider } from './useResizableLayout'
export type { PanelConfig, PanelState, ResizableContext } from './useResizableLayout'
export { useResizeObserver } from './useResizeObserver'
interface UseBglSchemaParamsT<T> {
	schema?: MaybeRefOrGetter<BglFormSchemaT<T>>
	columns?: MaybeRefOrGetter<string[]>
	data?: T[]
}

export function useBglSchema<T = { [key: string]: unknown }>(
	{ schema, columns, data }: UseBglSchemaParamsT<T> = {}
): BglFormSchemaT<T> {
	const _schema = computed(() => toValue(schema))
	const _columns = computed(() => toValue(columns))

	if (_schema.value) {
		return (
			_columns.value && _columns.value.length > 0
				? _schema.value.filter(f => _columns.value?.includes(f.id as string))
				: _schema.value
		) as BglFormSchemaT<T>
	}
	return getFallbackSchema(data, _columns.value)
}

export { useTheme } from './useTheme'
export { useValidateFieldValue } from './useValidateFieldValue'
