import { getBasicWidgetInstanceFunctions, getBooleanOrDefault, registerFactory } from '../../utils'
import type { BottomBarInstance, BottomBarProps } from './types'

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

/**
 * 底部交互/展示小部件响应式 Hook
 * @param props 组件参数
 * @returns 组件响应式数据
 */
export function useBottomBarWidget<T extends BottomBarInstance, P extends BottomBarProps>(
    props: P,
): [(instance: T) => void, BottomBarInstance] {
    const instanceRef = ref<T>()
    const register = registerFactory(instanceRef, props)
    const { get, setProps, toggleShow } = getBasicWidgetInstanceFunctions(instanceRef, name)

    return [
        register,
        {
            setProps,
            toggleShow,
            toggleExpand: (expand?: boolean) => get()?.toggleExpand(getBooleanOrDefault(expand)),
            height: computed(() => instanceRef?.value?.height) as unknown as ComputedRef<string>,
        },
    ]
}
