import { VG } from "../render/global";
import { viLoadLegacy } from "./load-legacy";

export const viLoad = (vi: VG, arg: { site_id: string; api_url: string }) => {
	vi.status = "loading";
	if (vi.on_status_changes) {
		vi.on_status_changes(vi.status);
	}
	vi.site.id = arg.site_id;
	vi.site.api_url = arg.api_url;

	if (!vi.site.api && !vi.site.db) {
		if (vi.site.code.mode === "old") {
			viLoadLegacy({
				site: {
					id: vi.site.id,
					api_url: vi.site.api_url,
					api: {
						get() {
							return vi.site.api;
						},
						set(val) {
							vi.site.api = val;
						},
					},
					db: {
						get() {
							return vi.site.db;
						},
						set(val) {
							vi.site.db = val;
						},
					},
				},
				render: vi.render,
			}).then(() => {
				vi.status = "ready";
				if (vi.on_status_changes) {
					vi.on_status_changes(vi.status);
				}
				vi.render();
			});
		} else {
		}
	} else {
		vi.status = "ready";
		if (vi.on_status_changes) {
			vi.on_status_changes(vi.status);
		}
	}
};
