"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.canvasClosePeers = exports.canvasPollPeers = exports.canvasOpenPeers = exports.canvasPageById = exports.canvasPageEdit = exports.canvasPageCreate = exports.allCanvasPages = exports.canvasPageByUrl = void 0;
const base_1 = require("@corejam/base");
const Client_1 = require("@corejam/base/dist/resolvers/db/fauna/Client");
const faunadb_1 = require("faunadb");
function canvasPageByUrl(slug) {
    return Client_1.FaunaClient()
        .query(faunadb_1.query.Get(faunadb_1.query.Match(faunadb_1.query.Index("seoSearch"), slug)))
        .then((response) => {
        return Object.assign({ id: response.ref.id }, response.data);
    });
}
exports.canvasPageByUrl = canvasPageByUrl;
function allCanvasPages() {
    return Client_1.FaunaClient()
        .query(faunadb_1.query.Map(faunadb_1.query.Paginate(faunadb_1.query.Match(faunadb_1.query.Index("allCanvasPages"))), faunadb_1.query.Lambda("x", faunadb_1.query.Merge({ id: faunadb_1.query.Select(["ref", "id"], faunadb_1.query.Get(faunadb_1.query.Var("x"))) }, faunadb_1.query.Select("data", faunadb_1.query.Get(faunadb_1.query.Var("x")))))))
        .then((response) => {
        return response.data;
    });
}
exports.allCanvasPages = allCanvasPages;
function canvasPageCreate(canvasPageInput) {
    return Client_1.FaunaClient()
        .query(faunadb_1.query.Create(faunadb_1.query.Collection("canvasPages"), {
        data: Object.assign(Object.assign({}, canvasPageInput), base_1.updateDates()),
    }))
        .then((response) => {
        return Object.assign({ id: response.ref.id }, response.data);
    });
}
exports.canvasPageCreate = canvasPageCreate;
function canvasPageEdit(id, canvasPageInput) {
    return Client_1.FaunaClient()
        .query(faunadb_1.query.Update(faunadb_1.query.Ref(faunadb_1.query.Collection("canvasPages"), id), {
        data: canvasPageInput,
    }))
        .then((response) => {
        return response.data;
    });
}
exports.canvasPageEdit = canvasPageEdit;
function canvasPageById(id) {
    return Client_1.FaunaClient()
        .query(faunadb_1.query.Get(faunadb_1.query.Ref(faunadb_1.query.Collection("canvasPages"), id)))
        .then((response) => {
        return Object.assign({ id: response.ref.id }, response.data);
    });
}
exports.canvasPageById = canvasPageById;
function canvasOpenPeers(id, peerInput) {
    return __awaiter(this, void 0, void 0, function* () {
        const canvas = (yield canvasPageById(id));
        const peers = {
            key: "test",
            peers: [],
        };
        if (!canvas.peers) {
            canvas.peers = peers;
        }
        canvas.peers.peers.push(Object.assign({}, peerInput));
        yield canvasPageEdit(id, Object.assign({}, canvas));
        return new Promise((res) => res(peers));
    });
}
exports.canvasOpenPeers = canvasOpenPeers;
function canvasPollPeers(id) {
    return __awaiter(this, void 0, void 0, function* () {
        const canvas = (yield canvasPageById(id));
        return new Promise((res) => { var _a; return res((_a = canvas.peers) !== null && _a !== void 0 ? _a : null); });
    });
}
exports.canvasPollPeers = canvasPollPeers;
function canvasClosePeers(id) {
    return __awaiter(this, void 0, void 0, function* () {
        const canvas = yield canvasPageById(id);
        canvas === null || canvas === void 0 ? true : delete canvas.peers;
        return Client_1.FaunaClient()
            .query(faunadb_1.query.Replace(faunadb_1.query.Ref(faunadb_1.query.Collection("canvasPages"), id), {
            data: Object.assign({}, canvas),
        }))
            .then((response) => {
            return response.data;
        });
    });
}
exports.canvasClosePeers = canvasClosePeers;
