"use strict";
/**
 * JupyterLab extension: Open in VS Code
 *
 * Adds an "Open in VS Code" button to the JupyterLab top bar.
 *
 * Clicking it:
 *  1. Calls /hub/api/users/{name}/vscode-connect to get a signed, short-lived URI
 *  2. Opens vscode://groundsada.jhub-vscode/connect?hub=...&token=...&user=...
 *  3. VS Code handles the rest:
 *     - Prompts to install "JupyterHub Remote" extension if not present
 *     - Downloads jhub-ssh binary if needed
 *     - Writes SSH config + opens remote window
 *
 * No setup, no CLI, no config required from the user.
 */
Object.defineProperty(exports, "__esModule", { value: true });
const coreutils_1 = require("@jupyterlab/coreutils");
const services_1 = require("@jupyterlab/services");
const widgets_1 = require("@lumino/widgets");
async function fetchConnectURI(username) {
    const settings = services_1.ServerConnection.makeSettings();
    const hubPrefix = coreutils_1.PageConfig.getOption('hubPrefix') || '/hub';
    const url = `${hubPrefix}/api/users/${encodeURIComponent(username)}/vscode-connect`;
    const response = await services_1.ServerConnection.makeRequest(url, { method: 'GET' }, settings);
    if (!response.ok) {
        const text = await response.text();
        throw new Error(`Hub API ${response.status}: ${text}`);
    }
    return response.json();
}
class VSCodeButton extends widgets_1.Widget {
    constructor(username) {
        super();
        this._username = username;
        this.addClass('jp-VSCodeButton');
        const btn = document.createElement('button');
        btn.title = 'Open in VS Code';
        btn.style.cssText = `
      display: flex; align-items: center; gap: 6px;
      padding: 4px 10px; cursor: pointer; border-radius: 4px;
      background: transparent; border: 1px solid var(--jp-border-color1);
      color: var(--jp-ui-font-color1); font-size: 13px;
      font-family: var(--jp-ui-font-family);
    `;
        btn.innerHTML = `
      <svg width="16" height="16" viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg" fill="currentColor">
        <path d="M74.8 4.9L40.6 34.7 17.3 18.5 5 25.3v49.4l12.3 6.8 23.3-16.2 34.2 29.8 18.2-8.7V13.6L74.8 4.9zM17.3 62.6V37.4l16.4 12.6-16.4 12.6zm39.4-.5L38.2 50l18.5-12.1V62.1z"/>
      </svg>
      Open in VS Code
    `;
        btn.addEventListener('click', () => void this._onClick());
        this.node.appendChild(btn);
    }
    async _onClick() {
        const btn = this.node.querySelector('button');
        btn.textContent = 'Connecting\u2026';
        btn.setAttribute('disabled', 'true');
        try {
            const info = await fetchConnectURI(this._username);
            // Open the vscode:// URI.
            // VS Code intercepts this and installs "JupyterHub Remote" from the
            // marketplace if not already present, then handles the connection.
            window.location.href = info.vscode_uri;
        }
        catch (err) {
            const msg = err instanceof Error ? err.message : String(err);
            console.error('[jhub-vscode]', msg);
            btn.textContent = '\u26a0 Failed';
            btn.title = msg;
            setTimeout(() => {
                btn.innerHTML = `
          <svg width="16" height="16" viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg" fill="currentColor">
            <path d="M74.8 4.9L40.6 34.7 17.3 18.5 5 25.3v49.4l12.3 6.8 23.3-16.2 34.2 29.8 18.2-8.7V13.6L74.8 4.9zM17.3 62.6V37.4l16.4 12.6-16.4 12.6zm39.4-.5L38.2 50l18.5-12.1V62.1z"/>
          </svg>
          Open in VS Code
        `;
                btn.removeAttribute('disabled');
                btn.title = 'Open in VS Code';
            }, 3000);
        }
        finally {
            btn.removeAttribute('disabled');
        }
    }
}
const plugin = {
    id: '@groundsada/jupyterhub-vscode:plugin',
    description: 'One-click "Open in VS Code" button for JupyterHub',
    autoStart: true,
    activate: (app) => {
        const hubUser = coreutils_1.PageConfig.getOption('hubUser');
        if (!hubUser) {
            console.log('[jhub-vscode] Not running under JupyterHub \u2014 skipping');
            return;
        }
        const button = new VSCodeButton(hubUser);
        app.shell.add(button, 'top', { rank: 1000 });
    },
};
exports.default = plugin;
//# sourceMappingURL=index.js.map