import { path } from "./deps.ts";
import initialCart from "./initialCart.json" assert { type: "json" };
import { Sheet } from "./sheet.ts";

let staticCart = initialCart as Array<Sheet>;
let cart: Array<Sheet> = JSON.parse(JSON.stringify(staticCart));

const virtualPathToRealPath = (virtualFname: string) => {
	const realPath = path.join(".", "carts", ...virtualFname.split("/"));
	return realPath;
}

export const saveCart = async (fname: string) => {
	await Deno.writeTextFile(virtualPathToRealPath(fname+".fx"), JSON.stringify(getCart()));
}

export const loadCart = async (fname: string) => {
	const json = await Deno.readTextFile(virtualPathToRealPath(fname+".fx"));
	staticCart = JSON.parse(json);
	cart = JSON.parse(json);
}

export const getCart = () => {
	return cart;
}