import { generateSeo } from "@corejam/base/dist/resolvers/db/faker/Generator";
import { random } from "faker";
import * as fs from "fs";
import { generateCanvasPage } from "./Generator";
export let canvasPages = [];
canvasPages.push({
    id: "static-canvas",
    seo: generateSeo({
        url: "canvas",
    }),
    canvas: `
  <html>
    <corejam-box>
      <corejam-type weight="bold">
        Static canvas test
      <corejam-type>
    </corejam-box>
  </html>`,
});
if (canvasPages.length === 0) {
    for (let index = 0; index < 5; index++) {
        const generated = generateCanvasPage({});
        canvasPages.push(Object.assign({ id: random.uuid() }, generated));
    }
}
export function canvasPageByUrl(slug) {
    const canvasPage = canvasPages.filter((canvasPage) => {
        var _a;
        if (((_a = canvasPage.seo) === null || _a === void 0 ? void 0 : _a.url) == slug) {
            return canvasPage;
        }
        return;
    })[0];
    return new Promise((res) => res(canvasPage));
}
export function allCanvasPages() {
    return new Promise((res) => res(canvasPages));
}
/**
 * When we are in faker mode we store in memory and also directly
 * write out to the www/build/config.json so the client can visit the
 * new page directly.
 *
 * @param canvasPageInput
 */
export function canvasPageCreate(canvasPageInput) {
    var _a;
    const model = Object.assign({ id: canvasPageInput.seo.url }, canvasPageInput);
    canvasPages.push(model);
    const configPath = process.cwd() + "/www/build/config.json";
    /**
     * Check are we inside corejam process
     * next.js does not have www/ folder
     */
    if (fs.existsSync(configPath)) {
        const config = require(configPath);
        config.router.routes.push({
            url: `/${(_a = canvasPageInput.seo) === null || _a === void 0 ? void 0 : _a.url}`,
            exact: true,
            canvasPage: true,
            component: canvasPageInput.canvas,
        });
        fs.writeFileSync(configPath, JSON.stringify(config));
    }
    return new Promise((res) => res(model));
}
export function canvasPageEdit(id, canvasPageInput) {
    let canvasPage = canvasPages.filter((canvasPage) => {
        return canvasPage.id === id;
    })[0];
    canvasPage = Object.assign(Object.assign({}, canvasPage), canvasPageInput);
    canvasPages = canvasPages.map((canvasPageOld) => {
        if (canvasPage.id === id) {
            return canvasPage;
        }
        return canvasPageOld;
    });
    return new Promise((res) => res(canvasPage));
}
export function canvasPageById(id) {
    return new Promise((res) => res(canvasPages.filter((canvasPage) => canvasPage.id === id)[0]));
}
export function canvasOpenPeers(id, peerInput) {
    let canvasPage = canvasPages.filter((canvasPage) => {
        return canvasPage.id === id;
    })[0];
    const peers = {
        key: "test",
        peers: [],
    };
    if (!canvasPage.peers) {
        canvasPage.peers = peers;
    }
    canvasPage.peers.peers.push({
        hash: peerInput.hash,
        offer: peerInput.offer,
    });
    canvasPage = Object.assign({}, canvasPage);
    canvasPages = canvasPages.map((canvasPageOld) => {
        if (canvasPage.id === id) {
            return canvasPage;
        }
        return canvasPageOld;
    });
    return new Promise((res) => res(peers));
}
export function canvasClosePeers(id) {
    let canvasPage = canvasPages.filter((canvasPage) => {
        return canvasPage.id === id;
    })[0];
    delete canvasPage.peers;
    canvasPage = Object.assign({}, canvasPage);
    canvasPages = canvasPages.map((canvasPageOld) => {
        if (canvasPage.id === id) {
            return canvasPage;
        }
        return canvasPageOld;
    });
    return new Promise((res) => res(canvasPage));
}
export function canvasPollPeers(id) {
    const canvasPage = canvasPages.filter((canvasPage) => {
        return canvasPage.id === id;
    })[0];
    return new Promise((res) => { var _a; return res((_a = canvasPage.peers) !== null && _a !== void 0 ? _a : null); });
}
