import {
  COURSEWARE_RUNTIME_CONFIG_PATH,
  LAB3_DEFAULT_TERMINAL_PATH,
  fetchCoursewareRuntimeConfig,
  getLab3TerminalPath as getSharedLab3TerminalPath,
  normalizeCoursewareRuntimeConfig,
  type CoursewareRuntimeConfig,
} from "~/lib/courseware-runtime";

export const LAB3_RUNTIME_CONFIG_PATH = COURSEWARE_RUNTIME_CONFIG_PATH;
export { LAB3_DEFAULT_TERMINAL_PATH };

export type Lab3RuntimeConfig = Pick<CoursewareRuntimeConfig, "lab3TerminalUrl">;

export type ResolvedLab3RuntimeConfig = {
  terminalPath: string;
};

export function getLab3TerminalPath(envValue?: string) {
  return getSharedLab3TerminalPath(envValue);
}

export function normalizeLab3RuntimeConfig(
  config?: Lab3RuntimeConfig,
): ResolvedLab3RuntimeConfig {
  const runtimeConfig = normalizeCoursewareRuntimeConfig(config);

  return {
    terminalPath: runtimeConfig.lab3TerminalUrl,
  };
}

export async function fetchLab3RuntimeConfig() {
  const runtimeConfig = await fetchCoursewareRuntimeConfig();

  return {
    terminalPath: runtimeConfig.lab3TerminalUrl,
  };
}
