import { registerWidgetFactory, unpackWidgetInstance } from '../../utils'
import type { AffixFilterInstance, AffixFilterProps, OptionItem } from './types'

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

/**
 * 吸附过滤器组件
 * @param props 组件参数
 * @returns 组件响应式数据
 */
export function useAffixFilterWidget<T extends AffixFilterInstance, P extends AffixFilterProps>(
    props: P,
): [(instance: T) => void, AffixFilterInstance] {
    const instanceRef = ref<T>()
    const register = registerWidgetFactory(instanceRef, props)
    const { get, ...fns } = unpackWidgetInstance(instanceRef, name)

    return [
        register,
        {
            ...fns,
            setOptionItem: (key: string, item: OptionItem) => get()?.setOptionItem(key, item),
        },
    ]
}
