const fs = require("fs");
const zlib = require("zlib");
const path = require("path");

const file = process.argv[2] || path.join(__dirname, "../assets/lottie/_joy.li");
const raw = fs.readFileSync(file);
console.log("len", raw.length, "head", raw.slice(0, 12).toString());

function tryDecode(name, fn) {
  try {
    const out = fn();
    const text = out.toString("utf8", 0, 300);
    const ok = text.includes('"v"') || text.includes('"layers"');
    console.log(name, ok ? "LOTTIE" : "no", "bytes", out.length, text.slice(0, 60).replace(/\s+/g, " "));
    if (ok) fs.writeFileSync(file.replace(/\.li$/, ".decoded.json"), out);
  } catch (e) {
    console.log(name, "fail", e.message);
  }
}

tryDecode("utf8-base64", () => Buffer.from(raw.toString("utf8").trim(), "base64"));
tryDecode("utf8-base64-gunzip", () => zlib.gunzipSync(Buffer.from(raw.toString("utf8").trim(), "base64")));
tryDecode("raw-gunzip", () => zlib.gunzipSync(raw));
tryDecode("raw-inflate", () => zlib.inflateSync(raw));
tryDecode("skip4-gunzip", () => zlib.gunzipSync(raw.slice(4)));
