import { getBooleanOrDefault, registerWidgetFactory, unpackWidgetInstance } from '../../utils'
import type { ToolBoxInstance, ToolBoxProps } from './types'

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

/**
 * 左侧工具盒子组件响应式 Hook
 * @param props 组件参数
 * @returns 组件响应式数据
 */
export function useToolBoxWidget<T extends ToolBoxInstance, P extends ToolBoxProps>(
    props: P,
): [(instance: T) => void, ToolBoxInstance] {
    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)),
        },
    ]
}
