"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.canvasPollPeers = exports.canvasClosePeers = exports.canvasOpenPeers = exports.canvasPageById = exports.canvasPageEdit = exports.canvasPageCreate = exports.allCanvasPages = exports.canvasPageByUrl = exports.canvasPages = void 0;
const Generator_1 = require("@corejam/base/dist/resolvers/db/faker/Generator");
const faker_1 = require("faker");
const fs = __importStar(require("fs"));
const Generator_2 = require("./Generator");
exports.canvasPages = [];
exports.canvasPages.push({
    id: "static-canvas",
    seo: Generator_1.generateSeo({
        url: "canvas",
    }),
    canvas: `
  <html>
    <corejam-box>
      <corejam-type weight="bold">
        Static canvas test
      <corejam-type>
    </corejam-box>
  </html>`,
});
if (exports.canvasPages.length === 0) {
    for (let index = 0; index < 5; index++) {
        const generated = Generator_2.generateCanvasPage({});
        exports.canvasPages.push(Object.assign({ id: faker_1.random.uuid() }, generated));
    }
}
function canvasPageByUrl(slug) {
    const canvasPage = exports.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));
}
exports.canvasPageByUrl = canvasPageByUrl;
function allCanvasPages() {
    return new Promise((res) => res(exports.canvasPages));
}
exports.allCanvasPages = allCanvasPages;
/**
 * 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
 */
function canvasPageCreate(canvasPageInput) {
    var _a;
    const model = Object.assign({ id: canvasPageInput.seo.url }, canvasPageInput);
    exports.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));
}
exports.canvasPageCreate = canvasPageCreate;
function canvasPageEdit(id, canvasPageInput) {
    let canvasPage = exports.canvasPages.filter((canvasPage) => {
        return canvasPage.id === id;
    })[0];
    canvasPage = Object.assign(Object.assign({}, canvasPage), canvasPageInput);
    exports.canvasPages = exports.canvasPages.map((canvasPageOld) => {
        if (canvasPage.id === id) {
            return canvasPage;
        }
        return canvasPageOld;
    });
    return new Promise((res) => res(canvasPage));
}
exports.canvasPageEdit = canvasPageEdit;
function canvasPageById(id) {
    return new Promise((res) => res(exports.canvasPages.filter((canvasPage) => canvasPage.id === id)[0]));
}
exports.canvasPageById = canvasPageById;
function canvasOpenPeers(id, peerInput) {
    let canvasPage = exports.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);
    exports.canvasPages = exports.canvasPages.map((canvasPageOld) => {
        if (canvasPage.id === id) {
            return canvasPage;
        }
        return canvasPageOld;
    });
    return new Promise((res) => res(peers));
}
exports.canvasOpenPeers = canvasOpenPeers;
function canvasClosePeers(id) {
    let canvasPage = exports.canvasPages.filter((canvasPage) => {
        return canvasPage.id === id;
    })[0];
    delete canvasPage.peers;
    canvasPage = Object.assign({}, canvasPage);
    exports.canvasPages = exports.canvasPages.map((canvasPageOld) => {
        if (canvasPage.id === id) {
            return canvasPage;
        }
        return canvasPageOld;
    });
    return new Promise((res) => res(canvasPage));
}
exports.canvasClosePeers = canvasClosePeers;
function canvasPollPeers(id) {
    const canvasPage = exports.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); });
}
exports.canvasPollPeers = canvasPollPeers;
