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());
    });
};
import { updateDates } from "@corejam/base";
import { FaunaClient } from "@corejam/base/dist/resolvers/db/fauna/Client";
import { query as q } from "faunadb";
export function canvasPageByUrl(slug) {
    return FaunaClient()
        .query(q.Get(q.Match(q.Index("seoSearch"), slug)))
        .then((response) => {
        return Object.assign({ id: response.ref.id }, response.data);
    });
}
export function allCanvasPages() {
    return FaunaClient()
        .query(q.Map(q.Paginate(q.Match(q.Index("allCanvasPages"))), q.Lambda("x", q.Merge({ id: q.Select(["ref", "id"], q.Get(q.Var("x"))) }, q.Select("data", q.Get(q.Var("x")))))))
        .then((response) => {
        return response.data;
    });
}
export function canvasPageCreate(canvasPageInput) {
    return FaunaClient()
        .query(q.Create(q.Collection("canvasPages"), {
        data: Object.assign(Object.assign({}, canvasPageInput), updateDates()),
    }))
        .then((response) => {
        return Object.assign({ id: response.ref.id }, response.data);
    });
}
export function canvasPageEdit(id, canvasPageInput) {
    return FaunaClient()
        .query(q.Update(q.Ref(q.Collection("canvasPages"), id), {
        data: canvasPageInput,
    }))
        .then((response) => {
        return response.data;
    });
}
export function canvasPageById(id) {
    return FaunaClient()
        .query(q.Get(q.Ref(q.Collection("canvasPages"), id)))
        .then((response) => {
        return Object.assign({ id: response.ref.id }, response.data);
    });
}
export 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));
    });
}
export 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); });
    });
}
export 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 FaunaClient()
            .query(q.Replace(q.Ref(q.Collection("canvasPages"), id), {
            data: Object.assign({}, canvas),
        }))
            .then((response) => {
            return response.data;
        });
    });
}
