"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.radioButtonPlugin = radioButtonPlugin;
const crypto_1 = __importDefault(require("crypto"));
const token_1 = __importDefault(require("markdown-it/lib/token"));
/**
 * Modified from https://github.com/revin/markdown-it-task-lists/blob/master/index.js
 */
function radioButtonPlugin(md, options) {
    const disableRadio = options ? !options.enabled : false;
    const useLabelWrapper = options ? !!options.label : true;
    md.core.ruler.after('inline', 'radio-lists', (state) => {
        const tokens = state.tokens;
        for (let i = 2; i < tokens.length; i++) {
            if (isTodoItem(tokens, i)) {
                const parentIdx = parentToken(tokens, i - 2);
                if (parentIdx === -1)
                    continue;
                const parent = tokens[parentIdx];
                let groupAttr = attrGet(parent, 'radio-group'); // try retrieve the group id
                let group;
                if (groupAttr) {
                    group = groupAttr[1];
                }
                else {
                    const hash = crypto_1.default.createHash('md5');
                    if (i >= 5 && tokens[i - 5]) {
                        hash.update(tokens[i - 5].content);
                    }
                    if (i >= 4 && tokens[i - 4]) {
                        hash.update(tokens[i - 4].content);
                    }
                    group = hash.update(tokens[i].content).digest('hex').slice(2, 7); // generate a deterministic group id
                }
                radioify(tokens[i], group, disableRadio, useLabelWrapper);
                attrSet(tokens[i - 2], 'class', 'radio-list-item');
                attrSet(parent, 'radio-group', group); // save the group id to the top-level list
                attrSet(parent, 'class', 'radio-list');
            }
        }
    });
}
function attrSet(token, name, value) {
    const index = token.attrIndex(name);
    const attr = [name, value];
    if (index < 0) {
        token.attrPush(attr);
    }
    else {
        if (token.attrs) {
            token.attrs[index] = attr;
        }
    }
}
function attrGet(token, name) {
    const index = token.attrIndex(name);
    if (index < 0 || !token.attrs) {
        return undefined;
    }
    return token.attrs[index];
}
function parentToken(tokens, index) {
    const targetLevel = tokens[index].level - 1;
    for (let i = index - 1; i >= 0; i--) {
        if (tokens[i].level === targetLevel) {
            return i;
        }
    }
    return -1;
}
function isTodoItem(tokens, index) {
    return isInline(tokens[index]) &&
        isParagraph(tokens[index - 1]) &&
        isListItem(tokens[index - 2]) &&
        startsWithTodoMarkdown(tokens[index]);
}
function radioify(token, radioId, disableRadio, useLabelWrapper) {
    if (!token.children)
        token.children = [];
    token.children.unshift(makeRadioButton(token, radioId, disableRadio));
    token.children[1].content = token.children[1].content.slice(3);
    token.content = token.content.slice(3);
    if (useLabelWrapper) {
        // Removed beingLabel & endLabel functions since we can just use new Token(...) now.
        token.children.unshift(new token_1.default('html_inline', '', 0));
        token.children[0].content = '<label>';
        token.children.push(new token_1.default('html_inline', '', 0));
        token.children[token.children.length - 1].content = '</label>';
    }
}
function makeRadioButton(token, radioId, disableRadio) {
    const radio = new token_1.default('html_inline', '', 0);
    const disabledAttr = disableRadio ? ' disabled="" ' : '';
    const isUnchecked = token.content.indexOf('( ) ') === 0;
    const isChecked = token.content.indexOf('(x) ') === 0 ||
        token.content.indexOf('(X) ') === 0;
    if (isUnchecked) {
        radio.content = `<input class="radio-list-input" name="${radioId}"${disabledAttr} type="radio">`;
    }
    else if (isChecked) {
        radio.content = `<input class="radio-list-input" checked="" name="${radioId}"${disabledAttr} type="radio">`;
    }
    return radio;
}
function isInline(token) { return token.type === 'inline'; }
function isParagraph(token) { return token.type === 'paragraph_open'; }
function isListItem(token) { return token.type === 'list_item_open'; }
function startsWithTodoMarkdown(token) {
    // leading whitespace in a list item is already trimmed off by markdown-it
    return token.content.indexOf('( ) ') === 0 || token.content.indexOf('(x) ') === 0 || token.content.indexOf('(X) ') === 0;
}
//# sourceMappingURL=markdown-it-radio-button.js.map