"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.linkOrMention = exports._fmt = exports.FmtString = void 0;
class FmtString {
    constructor(text, entities) {
        this.text = text;
        if (entities) {
            this.entities = entities;
            // force parse_mode to undefined if entities are present
            this.parse_mode = undefined;
        }
    }
    static normalise(content) {
        if (typeof content === 'string')
            return new FmtString(content);
        return content;
    }
}
exports.FmtString = FmtString;
function _fmt(kind, opts) {
    return function fmt(parts, ...items) {
        let text = '';
        const entities = [];
        parts = typeof parts === 'string' ? [parts] : parts;
        for (let i = 0; i < parts.length; i++) {
            // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
            text += parts[i];
            const item = items[i];
            if (!item)
                continue;
            if (typeof item === 'string') {
                text += item;
                continue;
            }
            for (const child of item.entities || [])
                entities.push({ ...child, offset: text.length + child.offset });
            text += item.text;
        }
        if (kind !== 'very-plain')
            entities.unshift({ type: kind, offset: 0, length: text.length, ...opts });
        return new FmtString(text, entities.length ? entities : undefined);
    };
}
exports._fmt = _fmt;
const linkOrMention = (content, data) => {
    const { text, entities = [] } = FmtString.normalise(content);
    entities.unshift(Object.assign(data, { offset: 0, length: text.length }));
    return new FmtString(text, entities);
};
exports.linkOrMention = linkOrMention;
