import superagent from 'superagent';
import { ILogObject, Logger } from "tslog";
import { appendFileSync } from "fs";

export const cookieParser = require("cookie-parser");

export { superagent };

export let titreBase = "SGA - Équipe 04";
export let serverAPI = "localhost:3200";

export async function api(uri: string, params: {}) {
  try {
    let response = await superagent
      .get(`http://${serverAPI}${uri}`)
      .query(params)
    return { code: response.status, body: response.body };
  } catch (error) {
    console.error(`Error API - ${error.message}`)
    return { code: error.status, message: error.body };
  }
}

// export function logger() {
export var consoleF1:Logger = new Logger({
    name: "console", 
    overwriteConsole: true,
    colorizePrettyLogs:true,
    dateTimeTimezone:"America/Montreal",
    dateTimePattern:"year/month/day - hours:minute:second",
    displayInstanceName:false,
    displayFilePath:'displayAll',
  
  });
  
  consoleF1.attachTransport(
    {
      silly: logToTransport,
      debug: logToTransport,
      trace: logToTransport,
      info: logToTransport,
      warn: logToTransport,
      error: logToTransport,
      fatal: logToTransport,
    },
    "debug"
  );
  function logToTransport(logObject: ILogObject) {
    appendFileSync("logs.txt", JSON.stringify(logObject) + "\n");
  }
//   return{
//     log: function (text:string) {
//       consoleF1.silly(text);
//     },
//     info: function (text:string) {
//       consoleF1.info(text);
//     },
//     trace: function (text:string) {
//       consoleF1.trace(text);
//     },
//     warn: function (text:string) {
//       consoleF1.warn(text);
//     },
//     error: function (text:string) {
//       consoleF1.error(text);
//     },
//     debug: function (text:string) {
//       consoleF1.debug(text);
//     },
//   }


// }

