#!/usr/bin/env node
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const execa_1 = __importDefault(require("execa"));
const fuse_js_1 = __importDefault(require("fuse.js"));
const lodash_1 = __importDefault(require("lodash"));
const meow_1 = __importDefault(require("meow"));
const path_1 = __importDefault(require("path"));
const term_img_1 = __importDefault(require("term-img"));
const pokemon_json_1 = __importDefault(require("../pokemon.json"));
const fuse = new fuse_js_1.default(Object.values(pokemon_json_1.default), {
    keys: [
        "prettyNames.eng",
        "prettyNames.chs",
        "prettyNames.jpn",
        "prettyNames.jpr_ro",
        "sprites",
        "names",
    ],
    isCaseSensitive: false,
    shouldSort: true,
});
const cli = meow_1.default(`
	Usage
	  $ pokemonshow <nameOrNumber>

	Options
		--xterm, -x  Outputs xterm instead of image in iTerm2.app
    --say Announces the name of the Pokémon
    --shiny Chooses the shiny sprite of the Pokémon (if available)
    --female Chooses the female sprite of the Pokémon (if available)
    --form Chooses a specific form of the Pokémon (if available)
    --gen8 Chooses the gen8 sprite of the Pokémon (if available)
    --list Lists all the available Pokémon
    --list-forms Lists all Pokémon with special forms
    --verbose, -v Outputs logs about the chosen Pokémon

  Examples
    $ pokemonshow
    $ pokemonshow rotom
    $ pokemonshow pikachu --form="gmax"
    $ pokemonshow porygon --shiny
    $ pokemonshow raichu --gen8 -x
    `, {
    autoHelp: true,
    flags: {
        xterm: {
            type: "boolean",
            alias: "x",
        },
        say: {
            type: "boolean",
        },
        list: {
            type: "boolean",
        },
        listForms: {
            type: "boolean",
        },
        shiny: {
            type: "boolean",
            default: false,
        },
        form: {
            type: "string",
            default: "$",
        },
        female: {
            type: "boolean",
            default: false,
        },
        gen8: {
            type: "boolean",
            default: false,
        },
        verbose: {
            type: "boolean",
            alias: "v",
        },
    },
});
function getPokemonFromInput(nameOrNumber) {
    if (!nameOrNumber) {
        return lodash_1.default.sample(pokemon_json_1.default);
    }
    const maybeNumber = Number(nameOrNumber);
    if (maybeNumber > 0 &&
        Number.isInteger(maybeNumber) &&
        Number.isFinite(maybeNumber)) {
        return pokemon_json_1.default.find((p) => p.number === maybeNumber);
    }
    else {
        const fuzzyResults = fuse.search(nameOrNumber);
        if (!fuzzyResults || fuzzyResults.length < 1) {
            return undefined;
        }
        return fuzzyResults[0].item;
    }
}
function chooseSprite(pokemon, flags) {
    const sorted = lodash_1.default(pokemon.sprites)
        .orderBy([
        (sprite) => {
            return sprite.form === flags.form.toLowerCase();
        },
        (sprite) => {
            return flags.female === sprite.female;
        },
        (sprite) => {
            return flags.gen8 === sprite.is_gen_8;
        },
        (sprite) => {
            return flags.shiny === sprite.shiny;
        },
    ], ["desc", "desc", "desc", "desc", "desc"])
        .value();
    if (flags.verbose) {
        console.log(sorted, flags);
    }
    return sorted[0].sprite;
}
async function displayImage(pokemon, flags) {
    if (flags.say) {
        console.log(`It's ${pokemon.prettyNames.eng}!`);
    }
    const chosenSprite = chooseSprite(pokemon, flags);
    const fallback = async () => {
        const { stdout } = await execa_1.default("cat", [
            path_1.default.resolve(__dirname, "..", ...chosenSprite
                .replace(".png", "")
                .replace("images/", "xterms/")
                .split("/")),
        ]);
        console.log(stdout);
    };
    if (flags.xterm) {
        return fallback();
    }
    term_img_1.default(path_1.default.resolve(__dirname, "..", ...chosenSprite.split("/")), {
        fallback,
    });
}
(async () => {
    if (cli.flags.list) {
        pokemon_json_1.default.forEach((pkmn) => {
            console.log(pkmn);
        });
        return;
    }
    if (cli.flags.listForms) {
        pokemon_json_1.default
            .filter((p) => p.sprites.some((s) => s.form !== "$"))
            .forEach((p) => console.log({
            number: p.number,
            name: p.prettyNames.eng,
            forms: lodash_1.default(p.sprites)
                .map((s) => s.form)
                .filter((s) => s !== "$")
                .uniq()
                .value(),
        }));
        return;
    }
    const name = cli.input[0];
    const pokemon = getPokemonFromInput(name);
    if (!pokemon) {
        return;
    }
    if (cli.flags.verbose) {
        console.log(pokemon);
    }
    displayImage(pokemon, cli.flags);
})();
