#!/usr/bin/env node
import _ from 'lodash';
import * as task from './task.js';
import Kink, { KinkList } from '@termsurf/kink';
import { makeBaseKinkText, makeKinkText } from '@termsurf/kink-text';
process.on('uncaughtException', kink => {
    if (kink instanceof KinkList) {
        if (kink.list.length === 1) {
            const k = kink.list[0];
            if (k) {
                Kink.saveFill(k);
                console.log(makeKinkText(k));
            }
        }
        else {
            console.log(makeKinkText(kink));
            kink.list.forEach(kink => {
                console.log(``);
                Kink.saveFill(kink);
                console.log(makeKinkText(kink));
            });
        }
    }
    else if (kink instanceof Kink) {
        Kink.saveFill(kink);
        console.log(makeKinkText(kink));
    }
    else if (kink instanceof Error) {
        console.log(makeBaseKinkText(kink));
    }
    else {
        console.log(kink);
    }
});
call(process.argv);
/**
 * Parse the input into an action and state.
 */
async function call(argv) {
    const input = read(argv);
    try {
        return await task.call(input.action, input);
    }
    catch (e) {
        if (e instanceof Kink) {
            console.log(makeKinkText(e));
        }
        else {
            throw e;
        }
    }
}
/**
 * Generically fetch CLI args.
 */
function read(argv) {
    let i = 2;
    const input = {
        action: argv[i++],
        object: [],
        detail: {},
    };
    while (i < argv.length) {
        const item = argv[i++];
        if (item.match(/^-+/)) {
            const name = item;
            const cased = name.match(/^--/)
                ? _.camelCase(name)
                : name.replace(/^\-+/, '');
            let value = argv[i++];
            if (!value) {
                value = true;
            }
            else if (value.match(/^-+/)) {
                i--;
                value = true;
            }
            const detail = (input.detail[cased] = input.detail[cased] || []);
            detail.push(value);
        }
        else {
            input.object.push(item);
        }
    }
    return input;
}
//# sourceMappingURL=index.js.map