"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.messageToString = exports.invisibleSpace = exports.space = exports.w = exports.p = exports.head = exports.error = exports.write = exports.markuplint = exports.uuid = exports.nonNullableFilter = void 0;
const tslib_1 = require("tslib");
const cli_color_1 = tslib_1.__importDefault(require("cli-color"));
// @ts-ignore
const eastasianwidth_1 = tslib_1.__importDefault(require("eastasianwidth"));
const strip_ansi_1 = tslib_1.__importDefault(require("strip-ansi"));
const uuid_1 = require("uuid");
function nonNullableFilter(item) {
    return !!item;
}
exports.nonNullableFilter = nonNullableFilter;
function uuid() {
    return (0, uuid_1.v4)();
}
exports.uuid = uuid;
const logo = `<${cli_color_1.default.xterm(39)('✔')}>`;
// eslint-disable-next-line @typescript-eslint/no-var-requires
const version = require('../package.json').version;
const eaw = eastasianwidth_1.default;
const box = (lines, { width = 40, padding = 1, center = false, noColor = false }) => {
    const bt = `┌${'─'.repeat(width - 2)}┐`;
    const pd = `│${' '.repeat(width - 2)}│`;
    const bb = `└${'─'.repeat(width - 2)}┘`;
    const texts = lines.map(line => {
        const nc = (0, strip_ansi_1.default)(line);
        const length = nc.length;
        const pad = width - 2 - 1 - length;
        const pad2 = Math.floor(pad / 2);
        const padD = pad % 2;
        const padL = center ? pad2 : 0;
        const padR = center ? pad2 + padD : pad;
        const text = noColor ? nc : line;
        return `│ ${' '.repeat(padL)}${text}${' '.repeat(padR)}│`;
    });
    const result = [bt, pd, ...texts, pd, bb];
    return result.join('\n');
};
exports.markuplint = `markup${cli_color_1.default.xterm(39)('lint')}`;
function write(message) {
    process.stdout.write(message + '\n');
}
exports.write = write;
write.break = () => process.stdout.write('\n');
function error(message) {
    process.stderr.write(message + '\n');
}
exports.error = error;
error.exit = () => process.exit(1);
const head = (method, noColor) => box([`${logo} ${exports.markuplint}`, cli_color_1.default.blackBright(`v${version}`), '', cli_color_1.default.bold(method)], {
    center: true,
    noColor,
});
exports.head = head;
function p(s, pad, start = false) {
    const l = w(`${s}`.trim());
    const d = pad - l;
    const _ = ' '.repeat(d < 0 ? 0 : d);
    return start ? `${_}${s}` : `${s}${_}`;
}
exports.p = p;
function w(s) {
    return s.replace(/./g, _ => '0'.repeat(eaw.characterLength(_))).length;
}
exports.w = w;
function space(str) {
    return str
        .replace(/\s+/g, $0 => {
        return cli_color_1.default.xterm(8)($0);
    })
        .replace(/ /g, $0 => '•')
        .replace(/\t/g, $0 => '→   ');
}
exports.space = space;
function invisibleSpace(str) {
    return str.replace(/\t/g, $0 => '    ').replace(/./g, $0 => ' ');
}
exports.invisibleSpace = invisibleSpace;
function messageToString(message, reason) {
    if (!reason) {
        return message;
    }
    return `${message} / ${reason}`;
}
exports.messageToString = messageToString;
