import store from "@/store";
import { storeMutations } from "@/constants/store-mutations";

export async function savePageData(page, data, merge = false) {
    const existingPageData = store.getters.pageData(page);

    let dataToSave;
    if (merge) {
        dataToSave = Object.assign(existingPageData ?? {}, data ?? {});
    } else {
        dataToSave = Object.keys(data).length > 0 ? data : (existingPageData ?? {});
    }

    // Save data if non-null
    store.commit(storeMutations.UPDATE_PAGE_DATA, { page: page, data: dataToSave });
}
