"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getVslotShorthandName = getVslotShorthandName;
exports.shiftSlotNodeDeeper = shiftSlotNodeDeeper;
exports.transformOldSlotSyntax = transformOldSlotSyntax;
const has_1 = __importDefault(require("lodash/has"));
const find_1 = __importDefault(require("lodash/find"));
const node_1 = require("../utils/node");
const _ = {
    has: has_1.default,
    find: find_1.default,
};
function getVslotShorthandName(node) {
    if (!node.attribs) {
        return '';
    }
    const keys = Object.keys(node.attribs);
    const vslotShorthand = _.find(keys, key => key.startsWith('#'));
    if (!vslotShorthand) {
        return '';
    }
    return vslotShorthand.substring(1, vslotShorthand.length); // remove #
}
/*
 * Shifts the slot node deeper by one level by creating a new intermediary node with template tag name.
 */
function shiftSlotNodeDeeper(node) {
    node.children.forEach((child) => {
        var _a;
        const vslotShorthandName = getVslotShorthandName(child);
        if (vslotShorthandName && child.name !== 'template') {
            const newSlotNode = (0, node_1.parseHTML)('<template></template>')[0];
            const vslotShorthand = `#${vslotShorthandName}`;
            newSlotNode.attribs[vslotShorthand] = '';
            if (child.attribs) {
                delete child.attribs[vslotShorthand];
            }
            newSlotNode.parent = node;
            child.parent = newSlotNode;
            newSlotNode.children = (_a = newSlotNode.children) !== null && _a !== void 0 ? _a : [];
            newSlotNode.children.push(child);
            // replace the shifted old child node with the new slot node
            node.children.forEach((childNode, idx) => {
                if (childNode === child) {
                    node.children[idx] = newSlotNode;
                }
            });
        }
    });
}
/*
 * Transforms deprecated vue slot syntax (slot="test") into the updated Vue slot shorthand syntax (#test).
 */
function transformOldSlotSyntax(node) {
    node.children.forEach((child) => {
        if (child.attribs && _.has(child.attribs, 'slot')) {
            const vslotShorthandName = `#${child.attribs.slot}`;
            child.attribs[vslotShorthandName] = '';
            delete child.attribs.slot;
        }
    });
}
//# sourceMappingURL=vueSlotSyntaxProcessor.js.map