import { modelsForPackage, elementForPackage, pieShortIdGenerator, elementsHasPackage } from "./utils/utils";
import cloneDeep from "lodash/cloneDeep";
/**
 * Allows you to modify the markup for a package that is present in elements/model but
 * missing from markup.
 * @param content the pie content
 * @param npmPackage the npm package
 * @param template a callback function for modifing the markup
 */
export const addMarkupForPackage = (content, npmPackage, template) => {
    const out = cloneDeep(content);
    const elem = out && elementForPackage(out, npmPackage);
    if (elem && modelsForPackage(out, npmPackage).length > 0) {
        const match = out.markup.match(new RegExp(elem));
        if (out.markup !== null && !match) {
            const id = out.models && out.models.find(m => m.element === elem).id;
            if (id) {
                out.markup = template(id, elem, out.markup);
            }
        }
    }
    return out;
};
/**
 * Adds rubric html to markup.
 * @param content
 */
export const addRubric = (content) => {
    return addMarkupForPackage(cloneDeep(content), "@pie-element/rubric", (id, tag, markup) => {
        return `
    ${markup}
    <div style="width: 75%">
      <${tag} id="${id}"></${tag}>
    </div>
    `;
    });
};
/**
 * Adds the provided package to the provided PieContent Object's `elements` and `models` properties.
 *
 * @param content the PieContent for rendering
 * @param packageToAdd the NPM Package to add to the content config
 * @param model optional the PieModel to add, `id` and `element` properties will be replaced by this function if present
 */
export const addPackageToContent = (content, packageToAdd, model) => {
    if (packageToAdd && !elementsHasPackage(content.elements, packageToAdd)) {
        model = model ? model : {};
        // add package
        model.id = pieShortIdGenerator();
        const elementName = pieShortIdGenerator();
        model.element = elementName;
        content.models && content.models.push(model);
        content.elements && (content.elements[elementName] = packageToAdd);
        return content;
    }
};
