import { getBooleanOrDefault, registerWidgetFactory, unpackWidgetInstance } 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 = registerWidgetFactory(instanceRef, props)
    const { get, ...fns } = unpackWidgetInstance(instanceRef, name)

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