#!/usr/bin/env node
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
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());
    });
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const http = __importStar(require("http"));
const path_1 = __importDefault(require("path"));
const fs_1 = require("fs");
const cors_1 = __importDefault(require("cors"));
const express_1 = __importDefault(require("express"));
const socket_io_1 = __importDefault(require("socket.io"));
const axios_1 = __importDefault(require("axios"));
const options_1 = require("./options");
const preview_1 = require("./preview");
const run = () => __awaiter(void 0, void 0, void 0, function* () {
    const { SERVER_PORT, FUNCTIONS_PATH, PREVIEW_CLIENT_PORT, PREVIEW_SERVER_PORT, } = yield options_1.getOptions();
    yield preview_1.initCode();
    const app = express_1.default();
    app.use(cors_1.default());
    app.get('/preview-client-ready', (request, response) => __awaiter(void 0, void 0, void 0, function* () {
        try {
            const { status } = yield axios_1.default.get('http://localhost:3000');
            response.sendStatus(status);
        }
        catch (_a) {
            response.sendStatus(500);
        }
    }));
    const server = http.createServer(app);
    const io = socket_io_1.default(server);
    io.origins('*:*');
    io.on('connection', (socket) => {
        const clientJsonPath = path_1.default.join(FUNCTIONS_PATH, 'client.json');
        const clientJson = JSON.parse(fs_1.readFileSync(clientJsonPath).toString());
        const packageJsonPath = path_1.default.join(FUNCTIONS_PATH, 'package.json');
        const packageJson = JSON.parse(fs_1.readFileSync(packageJsonPath).toString());
        const { openAPIEndpoint } = packageJson.uiStudio;
        socket.emit('init-client', clientJson);
        socket.emit('set-open-api-endpoint', openAPIEndpoint);
        socket.emit('set-server', {
            host: 'http://localhost',
            serverPort: PREVIEW_SERVER_PORT,
            clientPort: PREVIEW_CLIENT_PORT,
        });
        socket.on('init-builder', (r) => {
            socket.broadcast.emit('init-builder', r);
        });
        socket.on('init-api', (r) => {
            socket.broadcast.emit('init-api', r);
        });
        socket.on('elements-updated', (elements) => __awaiter(void 0, void 0, void 0, function* () {
            socket.broadcast.emit('update-tree', elements);
            fs_1.writeFileSync(clientJsonPath, JSON.stringify(Object.assign({ version: clientJson.version }, elements), null, 4));
        }));
        socket.on('navigate-page', (r) => {
            socket.broadcast.emit('navigate-page', r);
        });
        socket.on('select-element', (r) => {
            socket.broadcast.emit('select-element', r);
        });
        socket.on('hover-element', (r) => {
            socket.broadcast.emit('hover-element', r);
        });
    });
    app.use(express_1.default.static(path_1.default.join(__dirname, 'client')));
    app.get('/', (req, res) => {
        res.sendFile(path_1.default.join(__dirname, 'client', 'index.html'));
    });
    server.listen(SERVER_PORT, () => {
        console.log(`listening on *:${SERVER_PORT}`);
    });
});
run();
//# sourceMappingURL=server.js.map