import { View } from '..';
import webFileService from '../../service/webFile';
import { saveLastModified } from './hotReload';

/**
 * 保存子页面缓存
 * @param view
 */
export function saveViewCache(view: View) {
    const serviceId = view.page.service.id;
    const path = `ID_${view.id}.vue.json`;

    const promise = webFileService.saveFile({
        body: {
            serviceId,
            path,
            type: 'options-vue',
            content: JSON.stringify(view.toVueOptions({ finalCode: false })),
        },
    });

    saveLastModified(serviceId, path);
    return promise;
}
/**
 * 加载子页面缓存
 * @param view
 */
export async function loadViewCache(serviceId: string, viewId: string) {
    const result = await webFileService.loadFile({
        query: {
            serviceId,
            path: `ID_${viewId}.vue.json`,
        },
    });
    return result && JSON.parse(result.content);
}
