/*eslint quotes: ["error", "double", { "avoidEscape": true }]*/
import semver from "semver";
import { isUnreleasedVersion, prettifyVersion, semverify } from "./utils";

const wordEnds = size => {
  return size > 1 ? "s" : "";
};

export const logMessage = (message, context) => {
  const pre = context ? `[${context}] ` : "";
  const logStr = `  ${pre}${message}`;
  console.log(logStr);
};

export const logPlugin = (plugin, targets, list, context) => {
  const envList = list[plugin] || {};
  const filteredList = Object.keys(targets).reduce((a, b) => {
    const version = envList[b];
    const target = targets[b];

    if (!version) {
      a[b] = prettifyVersion(target);
    } else {
      const versionIsUnreleased = isUnreleasedVersion(version, b);
      const targetIsUnreleased = isUnreleasedVersion(target, b);

      if (
        (versionIsUnreleased && !targetIsUnreleased) ||
        (!targetIsUnreleased && semver.lt(target, semverify(version)))
      ) {
        a[b] = prettifyVersion(target);
      }
    }

    return a;
  }, {});

  const formattedTargets = JSON.stringify(filteredList)
    .replace(/,/g, ", ")
    .replace(/^\{"/, '{ "')
    .replace(/"\}$/, '" }');

  logMessage(`${plugin} ${formattedTargets}`, context);
};

export const logEntryPolyfills = (
  importPolyfillIncluded,
  polyfills,
  filename,
  onDebug,
) => {
  if (!importPolyfillIncluded) {
    console.log(
      `
[${filename}] \`import '@babel/polyfill'\` was not found.`,
    );
    return;
  }
  if (!polyfills.size) {
    console.log(
      `
[${filename}] Based on your targets, none were added.`,
    );
    return;
  }

  console.log(
    `
[${filename}] Replaced \`@babel/polyfill\` with the following polyfill${wordEnds(
      polyfills.size,
    )}:`,
  );
  onDebug(polyfills);
};

export const logUsagePolyfills = (polyfills, filename, onDebug) => {
  if (!polyfills.size) {
    console.log(
      `
[${filename}] Based on your code and targets, none were added.`,
    );
    return;
  }
  console.log(
    `
[${filename}] Added following polyfill${wordEnds(polyfills.size)}:`,
  );
  onDebug(polyfills);
};

@@ -381,24 +381,163 @@
 ogStr);%0A%7D;%0A%0A
+// Outputs a message that shows which target(s) caused an item to be included:%0A// transform-foo %7B %22edge%22:%2213%22, %22firefox%22:%2249%22, %22ie%22:%2210%22 %7D%0A
 export const
@@ -554,22 +554,20 @@
  = (
-plugin
+item
 , target
 s, l
@@ -562,16 +562,23 @@
 , target
+Version
 s, list,
@@ -604,23 +604,27 @@
 nst 
-envList
+minVersions
  = list%5B
 plug
@@ -619,22 +619,20 @@
  = list%5B
-plugin
+item
 %5D %7C%7C %7B%7D;
@@ -632,16 +632,17 @@
  %7C%7C %7B%7D;%0A
+%0A
   const 
@@ -674,16 +674,23 @@
 s(target
+Version
 s).reduc
@@ -696,12 +696,19 @@
 ce((
-a, b
+result, env
 ) =%3E
@@ -716,25 +716,28 @@
 %7B%0A    const 
-v
+minV
 ersion = env
@@ -737,17 +737,23 @@
 n = 
-envList%5Bb
+minVersions%5Benv
 %5D;%0A 
@@ -767,16 +767,23 @@
 t target
+Version
  = targe
@@ -787,11 +787,20 @@
 rget
-s%5Bb
+Versions%5Benv
 %5D;%0A%0A
@@ -808,17 +808,20 @@
    if (!
-v
+minV
 ersion) 
@@ -824,27 +824,34 @@
 on) %7B%0A      
-a%5Bb
+result%5Benv
 %5D = prettify
@@ -860,24 +860,31 @@
 rsion(target
+Version
 );%0A    %7D els
@@ -899,22 +899,18 @@
   const 
-versio
+mi
 nIsUnrel
@@ -941,17 +941,20 @@
 ion(
-v
+minV
 ersion, 
 b);%0A
@@ -949,17 +949,19 @@
 ersion, 
-b
+env
 );%0A     
@@ -1018,11 +1018,20 @@
 rget
-, b
+Version, env
 );%0A%0A
@@ -1052,32 +1052,8 @@
     
- (versionIsUnreleased &&
  !ta
@@ -1072,12 +1072,11 @@
 ased
-) %7C%7C
+ &&
 %0A   
@@ -1081,23 +1081,19 @@
        (
-!target
+min
 IsUnrele
@@ -1097,18 +1097,18 @@
 eleased 
-&&
+%7C%7C
  semver.
@@ -1116,16 +1116,23 @@
 t(target
+Version
 , semver
@@ -1135,17 +1135,20 @@
 mverify(
-v
+minV
 ersion))
@@ -1171,11 +1171,18 @@
     
-a%5Bb
+result%5Benv
 %5D = 
@@ -1203,16 +1203,23 @@
 n(target
+Version
 );%0A     
@@ -1239,17 +1239,22 @@
  return 
-a
+result
 ;%0A  %7D, %7B
@@ -1417,14 +1417,12 @@
 (%60$%7B
-plugin
+item
 %7D $%7B

Clean up logPlugin