import { getBasicWidgetInstanceFunctions, getBooleanOrDefault, registerFactory } 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 = registerFactory(instanceRef, props)
    const { get, setProps, toggleShow } = getBasicWidgetInstanceFunctions(instanceRef, name)

    return [
        register,
        {
            setProps,
            toggleShow,
            toggleExpand: (expand?: boolean) => get()?.toggleExpand(getBooleanOrDefault(expand)),
        },
    ]
}
