"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (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.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.registerApp = void 0;
const react_1 = __importDefault(require("react"));
const os_1 = require("os");
const cp = __importStar(require("child_process"));
const app_sdk_1 = require("@web-desktop-environment/app-sdk");
const server_api_1 = require("@web-desktop-environment/server-api");
const http_1 = __importDefault(require("http"));
class VSCode extends app_sdk_1.AppBase {
    constructor() {
        super(...arguments);
        this.name = "vscode";
        this.state = {
            isLoaded: false,
            useDefaultWindow: true,
            defaultWindowTitle: "vs-code",
        };
        this.willUnmount = false;
        this.runVsCodeCli = (port, domain) => {
            this.vscode = cp.spawn(process.execPath, [
                require.resolve("code-server/out/node/entry.js"),
                `--port=${port}`,
                "--auth=none",
                "--host=0.0.0.0",
                process.cwd() + "/",
            ], {
                stdio: ["ipc"],
                env: {
                    PUBLIC_URL: `/${domain}/*/`,
                },
            });
            server_api_1.APIClient.addChildProcess(this.vscode);
            const waitForVscodeToLoad = () => {
                if (!this.willUnmount) {
                    http_1.default
                        .get(`http://localhost:${port}/`, (res) => {
                        this.setState({ isLoaded: true });
                    })
                        .on("error", (e) => {
                        setTimeout(waitForVscodeToLoad, 100);
                    });
                }
            };
            waitForVscodeToLoad();
        };
        this.componentWillUnmount = () => {
            this.willUnmount = true;
            this.vscode.kill();
        };
        this.componentDidMount = () => {
            this.api.portManager.withDomain().then(({ port, domain }) => {
                this.runVsCodeCli(port, domain);
                this.setState({ port, id: domain });
            });
        };
        this.renderApp = ({ Iframe, LoadingScreen, }) => {
            const { id, isLoaded } = this.state;
            return id && isLoaded ? (react_1.default.createElement(Iframe, { id: id, type: "internal" })) : (react_1.default.createElement(LoadingScreen, { message: "loading vs-code", variant: "jumpCube" }));
        };
    }
}
const registerApp = () => app_sdk_1.AppsManager.registerApp({
    vscode: {
        displayName: "VS-Code",
        description: "full vscode editor",
        App: VSCode,
        defaultInput: { location: (0, os_1.homedir)() },
        icon: {
            type: "icon",
            icon: "VscCode",
        },
        color: "#219CF0",
        window: {
            height: 700,
            width: 1000,
            position: { x: 50, y: 50 },
            maxHeight: 7000,
            maxWidth: 7000,
            minWidth: 500,
            minHeight: 500,
            allowLocalScreenSnapping: true,
        },
    },
});
exports.registerApp = registerApp;
//# sourceMappingURL=VisualStudioCode.js.map