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 { generateApolloClient } from '@deep-foundation/hasura/client.js';
import { HasuraApi } from '@deep-foundation/hasura/api.js';
import { generateMutation, generateSerial, insertMutation } from '../gql/index.js';
import Debug from 'debug';
const debug = Debug('deeplinks:eh:reserved');
const log = debug.extend('log');
const error = debug.extend('error');
const SCHEMA = 'public';
export const api = new HasuraApi({
    path: process.env.DEEPLINKS_HASURA_PATH,
    ssl: !!+process.env.DEEPLINKS_HASURA_SSL,
    secret: process.env.DEEPLINKS_HASURA_SECRET,
});
const client = generateApolloClient({
    path: `${process.env.DEEPLINKS_HASURA_PATH}/v1/graphql`,
    ssl: !!+process.env.DEEPLINKS_HASURA_SSL,
    secret: process.env.DEEPLINKS_HASURA_SECRET,
});
export default (req, res) => __awaiter(void 0, void 0, void 0, function* () {
    var _a, _b, _c, _d, _e, _f, _g;
    try {
        const count = (_b = (_a = req === null || req === void 0 ? void 0 : req.body) === null || _a === void 0 ? void 0 : _a.input) === null || _b === void 0 ? void 0 : _b.count;
        if (!count)
            return res.status(500).json({ error: 'no count' });
        const links = [];
        for (let i = 0; i < count; i++)
            links[i] = { type_id: 0 };
        const mutateLinksResult = yield client.mutate(generateSerial({
            actions: [insertMutation('links', { objects: links })],
            name: 'INSERT_LINKS',
        }));
        const ids = (_d = (_c = mutateLinksResult.data['m0']) === null || _c === void 0 ? void 0 : _c.returning) === null || _d === void 0 ? void 0 : _d.map(node => node.id);
        if (!ids)
            res.status(500).json({ error: 'insert links error' });
        const mutateReservedResult = yield client.mutate(generateSerial({
            actions: [
                generateMutation({
                    tableName: 'reserved', operation: 'insert',
                    variables: { objects: { reserved_ids: ids, user_id: 123123, created_at: new Date().toISOString() } },
                }),
            ],
            name: 'INSERT_RESERVED',
        }));
        if (!((_g = (_f = (_e = mutateLinksResult.data['m0']) === null || _e === void 0 ? void 0 : _e.returning) === null || _f === void 0 ? void 0 : _f[0]) === null || _g === void 0 ? void 0 : _g.id))
            res.status(500).json({ error: 'insert reserved error' });
        return res.json({ ids });
    }
    catch (e) {
        error(JSON.stringify(e, null, 2));
        return res.status(500).json({ error: e.toString() });
    }
});
//# sourceMappingURL=reserved.js.map