// SPDX-License-Identifier: AGPL-3.0-only
// Copyright (C) 2026 Giancarlo Erra - Altaire Limited

import * as vscode from "vscode";
import { getSettings } from "./settings.js";

/**
 * Status-bar item that opens the SocratiCode sidebar on click.
 * Honours the `socraticode.statusBar` setting.
 */

let item: vscode.StatusBarItem | undefined;

export function registerStatusBar(context: vscode.ExtensionContext): void {
  item = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Right, 50);
  item.command = "workbench.view.extension.socraticode";
  context.subscriptions.push(item);

  refresh();

  context.subscriptions.push(
    vscode.workspace.onDidChangeConfiguration((e) => {
      if (e.affectsConfiguration("socraticode.statusBar")) {
        refresh();
      }
    }),
  );
}

function refresh(): void {
  if (!item) return;
  if (!getSettings().showStatusBar) {
    item.hide();
    return;
  }
  item.text = "$(server) SocratiCode";
  item.tooltip = "Click to open the SocratiCode sidebar.";
  item.show();
}
