/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */
/* vim: set ft=javascript ts=2 et sw=2 tw=80: */
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */
/* import-globals-from ../../../../framework/test/shared-head.js */

"use strict";

// shared-head.js handles imports, constants, and utility functions
// Load the shared-head file first.
Services.scriptloader.loadSubScript(
  "chrome://mochitests/content/browser/devtools/client/framework/test/shared-head.js",
  this);

Services.prefs.setBoolPref("devtools.webconsole.new-frontend-enabled", true);
registerCleanupFunction(() => {
  Services.prefs.clearUserPref("devtools.webconsole.new-frontend-enabled");
});

const { prepareMessage } = require("devtools/client/webconsole/new-console-output/utils/messages");

const BASE_PATH = "../../../../devtools/client/webconsole/new-console-output/test/fixtures";
const TEMP_FILE_PATH = OS.Path.join(`${BASE_PATH}/stub-generators`, "test-tempfile.js");

function formatPacket(key, packet) {
  return `
stubPackets.set("${key}", ${JSON.stringify(packet, null, "\t")});
`;
}

function formatStub(key, message) {
  let prepared = prepareMessage(message, {getNextId: () => "1"});
  return `
stubPreparedMessages.set("${key}", new ConsoleMessage(${JSON.stringify(prepared, null, "\t")}));
`;
}

function formatNetworkStub(key, packet) {
  let actor = packet.eventActor;
  let networkInfo = {
    _type: "NetworkEvent",
    timeStamp: actor.timeStamp,
    node: null,
    actor: actor.actor,
    discardRequestBody: true,
    discardResponseBody: true,
    startedDateTime: actor.startedDateTime,
    request: {
      url: actor.url,
      method: actor.method,
    },
    isXHR: actor.isXHR,
    cause: actor.cause,
    response: {},
    timings: {},
    // track the list of network event updates
    updates: [],
    private: actor.private,
    fromCache: actor.fromCache,
    fromServiceWorker: actor.fromServiceWorker
  };
  let prepared = prepareMessage(networkInfo, {getNextId: () => "1"});
  return `
stubPreparedMessages.set("${key}", new NetworkEventMessage(${JSON.stringify(prepared, null, "\t")}));
`;
}

function formatFile(stubs) {
  return `/* Any copyright is dedicated to the Public Domain.
  http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

/*
 * THIS FILE IS AUTOGENERATED. DO NOT MODIFY BY HAND. RUN TESTS IN FIXTURES/ TO UPDATE.
 */

const { ConsoleMessage, NetworkEventMessage } = require("devtools/client/webconsole/new-console-output/types");

let stubPreparedMessages = new Map();
let stubPackets = new Map();

${stubs.preparedMessages.join("")}
${stubs.packets.join("")}

module.exports = {
  stubPreparedMessages,
  stubPackets,
}`;
}
