import { createServer } from "http";
import { KomandNaExport, SRecord } from "./types";

createServer((_, res) => {
  const komandy: KomandNaExport[] = module.exports.komandy;
  const customKomandy: KomandNaExport[] = module.exports.customKomandy;
  const customAliasy: SRecord<string> = module.exports.customAliasy;

  const vsechnyKomandy: KomandNaExport[] = JSON.parse(JSON.stringify([...komandy, ...customKomandy]));

  for (const key in customAliasy) {
    const aliasParts = customAliasy[key].split(" ");
    const aliasCmd = aliasParts[0];

    const i = vsechnyKomandy.findIndex(komand => komand.name == aliasCmd);
    if (i == -1) continue;
    (vsechnyKomandy[i].als ??= []).push(aliasParts.length == 1 ? key : `${key} [${aliasParts.slice(1).join(" ")}]`);
  }

  vsechnyKomandy.sort((a, b) => a.name < b.name ? -1 : a.name > b.name ? 1 : 0);

  const odpoved = JSON.stringify({ komandy: vsechnyKomandy });
  res.setHeader("Content-Type", "application/json");
  res.end(odpoved);
}).listen(5712);
