import { ref } from 'vue'

/**
 * @param callback 组件关闭时的回调
 * @returns 
 */
export function useComponent(callback?: (componentName?: string) => void) {
    // 组件名
    const componentId = ref<string>();

    /**
     * 打开组件
     * @param componentName 
     */
    const openComponent = (componentName: string) => {
        if (componentName) {
            console.log('打开组件：' + componentName.toString());
            componentId.value = componentName;
        } else {
            console.error('无效的组件名');
        }
    }

    /**
     * 关闭组件
     * @param isCallback 
     */
    const closeComponent = (isCallback?: boolean) => {
        const componentName = componentId.value
        componentId.value = undefined;
        console.log('关闭组件：' + componentName);

        // 是否刷新数据
        if (isCallback && callback) {
            callback(componentName);
        }
    }

    return {
        componentId,
        openComponent,
        closeComponent,
    }
}