import { registerWidgetFactory, unpackWidgetInstance } from '../../utils'
import type { SwitchControlInstance, SwitchControlProps } from './types'

// 组件名称
export const name = 'SwitchControlWidget'

/**
 * 前后切换组件响应式 Hook
 * @param props 组件参数
 * @returns 组件响应式数据
 */
export function useSwitchWidget<T extends SwitchControlInstance, P extends SwitchControlProps>(
    props: P,
): [(instance: T) => void, SwitchControlInstance] {
    const instanceRef = ref<T>()
    const register = registerWidgetFactory(instanceRef, props)
    const { get, ...fns } = unpackWidgetInstance(instanceRef, name)

    return [
        register,
        {
            ...fns,
            prev: () => get()?.prev(),
            next: () => get()?.next(),
        },
    ]
}
