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 { DeepClient } from "./imports/client.js";
import axios from 'axios';
import Debug from 'debug';
import { serializeError } from 'serialize-error';
const debug = Debug('deeplinks:warmup');
const apolloClient = generateApolloClient({
    path: `${process.env.DEEPLINKS_HASURA_PATH}/v1/graphql`,
    ssl: !!+process.env.DEEPLINKS_HASURA_SSL,
    secret: process.env.DEEPLINKS_HASURA_SECRET,
});
const deep = new DeepClient({ apolloClient });
export const checkSystemStatus = () => __awaiter(void 0, void 0, void 0, function* () {
    var _a, _b, _c;
    try {
        const status = yield axios.post(`http://localhost:3006/gql`, { "query": "{ healthz { status } }" }, { validateStatus: status => true, timeout: 7000 });
        return { result: (_c = (_b = (_a = status === null || status === void 0 ? void 0 : status.data) === null || _a === void 0 ? void 0 : _a.data) === null || _b === void 0 ? void 0 : _b.healthz) === null || _c === void 0 ? void 0 : _c[0].status };
    }
    catch (e) {
        const serializedError = serializeError(e);
        return { error: serializedError };
    }
});
(() => __awaiter(void 0, void 0, void 0, function* () {
    var _d, _e, _f;
    let systemIsReady = false;
    while (!systemIsReady) {
        systemIsReady = (yield checkSystemStatus()).result === 'ok';
    }
    const stringClientHandlerId = yield deep.id("@deep-foundation/deepcase", "stringClientHandler");
    const stringClientHandler = (yield deep.select(stringClientHandlerId));
    let jsx = (_f = (_e = (_d = stringClientHandler === null || stringClientHandler === void 0 ? void 0 : stringClientHandler.data) === null || _d === void 0 ? void 0 : _d[0]) === null || _e === void 0 ? void 0 : _e.value) === null || _f === void 0 ? void 0 : _f.value;
    if (jsx) {
        var lastChar = jsx.substr(jsx.length - 1);
        if (lastChar == '}') {
            jsx += " ";
        }
        else {
            jsx = jsx.trim();
        }
        yield deep.update({ link_id: stringClientHandlerId }, { value: jsx }, { table: `strings` });
        console.log('JavaScript Docker Isolation Provider startup triggered.');
    }
    else {
        console.log('Link value to trigger JavaScript Docker Isolation Provider startup is not found.');
    }
}))();
//# sourceMappingURL=warmup.js.map