"use strict";
(() => {
var exports = {};
exports.id = 702;
exports.ids = [702];
exports.modules = {

/***/ 9460:
/***/ ((module) => {

module.exports = require("lokijs");

/***/ }),

/***/ 9267:
/***/ ((module) => {

module.exports = require("nextjs-cors");

/***/ }),

/***/ 3906:
/***/ ((module) => {

module.exports = import("docmq");;

/***/ }),

/***/ 2353:
/***/ ((module) => {

module.exports = import("luxon");;

/***/ }),

/***/ 6555:
/***/ ((module) => {

module.exports = import("uuid");;

/***/ }),

/***/ 870:
/***/ ((module, __webpack_exports__, __webpack_require__) => {

__webpack_require__.a(module, async (__webpack_handle_async_dependencies__, __webpack_async_result__) => { try {
/* harmony export */ __webpack_require__.d(__webpack_exports__, {
/* harmony export */   "U": () => (/* binding */ cancelJob)
/* harmony export */ });
/* harmony import */ var db_mq__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(7947);
/* harmony import */ var db_loki__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(4276);
var __webpack_async_dependencies__ = __webpack_handle_async_dependencies__([db_mq__WEBPACK_IMPORTED_MODULE_0__]);
db_mq__WEBPACK_IMPORTED_MODULE_0__ = (__webpack_async_dependencies__.then ? (await __webpack_async_dependencies__)() : __webpack_async_dependencies__)[0];


const cancelJob = async (variables, context)=>{
    const id = context.v5(variables.name);
    const queue = await (0,db_mq__WEBPACK_IMPORTED_MODULE_0__/* .getQueue */ .w)();
    const col = (0,db_loki__WEBPACK_IMPORTED_MODULE_1__/* .getCollection */ ._)("tds-jobs");
    let doc;
    await queue.transaction(async (q)=>{
        var ref;
        // remove
        await q.remove(variables.name);
        doc = (ref = col.chain().find({
            id
        }).update((doc)=>{
            doc.enabled = false;
            return doc;
        }).data()) === null || ref === void 0 ? void 0 : ref[0];
    });
    if (typeof doc === "undefined") {
        // nothing to remove, null cancelJob
        return {
            cancelJob: null
        };
    }
    return {
        cancelJob: {
            id: doc.id,
            name: doc.name,
            endpoint: doc.endpoint,
            enabled: true,
            retries: doc.retries,
            runAt: doc.runAt,
            runEvery: doc.runEvery,
            headers: doc.headers ? JSON.parse(doc.headers) : null,
            body: doc.body
        }
    };
};

__webpack_async_result__();
} catch(e) { __webpack_async_result__(e); } });

/***/ }),

/***/ 3088:
/***/ ((module, __webpack_exports__, __webpack_require__) => {

__webpack_require__.a(module, async (__webpack_handle_async_dependencies__, __webpack_async_result__) => { try {
/* harmony export */ __webpack_require__.d(__webpack_exports__, {
/* harmony export */   "C": () => (/* binding */ enqueueJob)
/* harmony export */ });
/* harmony import */ var luxon__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(2353);
/* harmony import */ var db_mq__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(7947);
/* harmony import */ var db_loki__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(4276);
var __webpack_async_dependencies__ = __webpack_handle_async_dependencies__([luxon__WEBPACK_IMPORTED_MODULE_0__, db_mq__WEBPACK_IMPORTED_MODULE_1__]);
([luxon__WEBPACK_IMPORTED_MODULE_0__, db_mq__WEBPACK_IMPORTED_MODULE_1__] = __webpack_async_dependencies__.then ? (await __webpack_async_dependencies__)() : __webpack_async_dependencies__);



const enqueueJob = async (variables, context)=>{
    const id = context.v5(variables.name);
    let runAt = luxon__WEBPACK_IMPORTED_MODULE_0__.DateTime.now();
    if (typeof variables.job.runAt === "string" && variables.job.runAt !== "") {
        const dt = luxon__WEBPACK_IMPORTED_MODULE_0__.DateTime.fromISO(variables.job.runAt);
        if (dt.isValid) {
            runAt = dt;
        }
    }
    const retries = variables.job.retries ?? 5;
    const headers = !Array.isArray(variables.job.headers) ? {} : variables.job.headers.reduce((h, next)=>{
        h[next.name] = next.value;
        return h;
    }, {});
    const queue = await (0,db_mq__WEBPACK_IMPORTED_MODULE_1__/* .getQueue */ .w)();
    const col = (0,db_loki__WEBPACK_IMPORTED_MODULE_2__/* .getCollection */ ._)("tds-jobs");
    let doc;
    await queue.transaction(async (q)=>{
        // job
        await q.enqueue({
            ref: id,
            payload: {
                queueName: context.queueName,
                projectId: context.projectId,
                jobId: id,
                name: variables.name,
                endpoint: variables.job.endpoint,
                headers: JSON.stringify(headers),
                body: variables.job.body ?? null,
                method: variables.job.method ?? "POST"
            },
            retries: variables.job.retries ?? undefined,
            runAt: runAt.toJSDate(),
            runEvery: variables.job.runEvery ?? undefined,
            timezone: variables.job.timezone ?? undefined
        });
        const existing = col.findOne({
            id
        });
        const next = {
            id,
            name: variables.name,
            endpoint: variables.job.endpoint,
            enabled: true,
            headers: JSON.stringify(headers),
            method: variables.job.method ?? "POST",
            body: variables.job.body ?? null,
            retries,
            runAt: runAt.toISO(),
            runEvery: variables.job.runEvery ?? undefined,
            timezone: variables.job.timezone ?? undefined
        };
        if (existing) {
            col.chain().find({
                id
            }).remove();
        }
        // insert (upsertish)
        col.insertOne(next);
        doc = next;
    });
    if (typeof doc === "undefined") {
        throw new Error("No doc");
    }
    return {
        enqueueJob: {
            id: doc.id,
            name: doc.name,
            endpoint: doc.endpoint,
            enabled: true,
            retries,
            runAt: runAt.toISO(),
            runEvery: doc.runEvery,
            headers: variables.job.headers,
            body: variables.job.body,
            timezone: variables.job.timezone
        }
    };
};

__webpack_async_result__();
} catch(e) { __webpack_async_result__(e); } });

/***/ }),

/***/ 877:
/***/ ((module, __webpack_exports__, __webpack_require__) => {

__webpack_require__.a(module, async (__webpack_handle_async_dependencies__, __webpack_async_result__) => { try {
__webpack_require__.r(__webpack_exports__);
/* harmony export */ __webpack_require__.d(__webpack_exports__, {
/* harmony export */   "default": () => (/* binding */ MockGraphqlServer)
/* harmony export */ });
/* harmony import */ var graphql_operations_cancelJob__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(870);
/* harmony import */ var graphql_operations_enqueueJob__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(3088);
/* harmony import */ var nextjs_cors__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(9267);
/* harmony import */ var nextjs_cors__WEBPACK_IMPORTED_MODULE_2___default = /*#__PURE__*/__webpack_require__.n(nextjs_cors__WEBPACK_IMPORTED_MODULE_2__);
/* harmony import */ var uuid__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__(6555);
var __webpack_async_dependencies__ = __webpack_handle_async_dependencies__([graphql_operations_cancelJob__WEBPACK_IMPORTED_MODULE_0__, graphql_operations_enqueueJob__WEBPACK_IMPORTED_MODULE_1__, uuid__WEBPACK_IMPORTED_MODULE_3__]);
([graphql_operations_cancelJob__WEBPACK_IMPORTED_MODULE_0__, graphql_operations_enqueueJob__WEBPACK_IMPORTED_MODULE_1__, uuid__WEBPACK_IMPORTED_MODULE_3__] = __webpack_async_dependencies__.then ? (await __webpack_async_dependencies__)() : __webpack_async_dependencies__);
// Next.js API route support: https://nextjs.org/docs/api-routes/introduction




const nilId = "00000000-0000-0000-0000-000000000000";
/** A simplified mock graphql server that emulates for.taskless.io calls */ async function MockGraphqlServer(req, res) {
    var ref, ref1;
    await nextjs_cors__WEBPACK_IMPORTED_MODULE_2___default()(req, res, {
        methods: [
            "GET",
            "HEAD",
            "POST"
        ],
        origin: "*",
        optionsSuccessStatus: 204
    });
    // gql items from request body
    // const operationName: string = req.body?.operationName ?? "";
    const query = (((ref = req.body) === null || ref === void 0 ? void 0 : ref.query) ?? "").trim();
    const variables = ((ref1 = req.body) === null || ref1 === void 0 ? void 0 : ref1.variables) ?? {};
    const projectId = Array.isArray(req.headers["x-taskless-id"]) ? req.headers["x-taskless-id"][0] : req.headers["x-taskless-id"];
    const queueName = Array.isArray(req.headers["x-taskless-queue"]) ? req.headers["x-taskless-queue"][0] : req.headers["x-taskless-queue"];
    const context = {
        projectId: typeof projectId === "string" && projectId.length > 0 ? projectId : nilId,
        queueName: typeof queueName === "string" && queueName.length > 0 ? queueName : "default",
        v5: (value)=>(0,uuid__WEBPACK_IMPORTED_MODULE_3__.v5)(value, projectId ?? nilId)
    };
    // collected results
    let results = {};
    if (/mutation\s+cancelJob\(/.test(query)) {
        const r = await (0,graphql_operations_cancelJob__WEBPACK_IMPORTED_MODULE_0__/* .cancelJob */ .U)(variables, context);
        results = {
            ...results,
            ...r
        };
    }
    if (/mutation\s+enqueueJob\(/.test(query)) {
        const r1 = await (0,graphql_operations_enqueueJob__WEBPACK_IMPORTED_MODULE_1__/* .enqueueJob */ .C)(variables, context);
        results = {
            ...results,
            ...r1
        };
    }
    res.status(200).json({
        data: results
    });
}

__webpack_async_result__();
} catch(e) { __webpack_async_result__(e); } });

/***/ })

};
;

// load runtime
var __webpack_require__ = require("../../webpack-api-runtime.js");
__webpack_require__.C(exports);
var __webpack_exec__ = (moduleId) => (__webpack_require__(__webpack_require__.s = moduleId))
var __webpack_exports__ = __webpack_require__.X(0, [947], () => (__webpack_exec__(877)));
module.exports = __webpack_exports__;

})();