"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ensureUniqueEntitySlug = void 0;
const slugs_1 = require("../slugs");
const slugs_2 = require("./slugs");
// NOTE: this mutates the entity that is passed in
const ensureUniqueEntitySlug = async (entity, requestOptions) => {
    let currentSlug = entity.slug;
    const orgUrlKey = entity.orgUrlKey.toLowerCase();
    // if we got the entity from the entity editor, the slug will already have the orgUrlKey prefix
    // but it might not have been edited with the entity editor...
    // ie a dev might have just set the slug to "my-slug" and not "org|my-slug" (like i did)
    if (currentSlug && !currentSlug.startsWith(`${orgUrlKey}|`)) {
        currentSlug = (0, slugs_2.truncateSlug)(currentSlug, orgUrlKey);
    }
    // verify that the slug is unique
    const { id: existingId } = entity;
    const slugInfo = existingId
        ? { slug: currentSlug, existingId }
        : { slug: currentSlug };
    entity.slug = await (0, slugs_1.getUniqueSlug)(slugInfo, requestOptions);
    // add slug to keywords
    entity.typeKeywords = (0, slugs_1.setSlugKeyword)(entity.typeKeywords, entity.slug);
    return entity;
};
exports.ensureUniqueEntitySlug = ensureUniqueEntitySlug;
//# sourceMappingURL=ensureUniqueEntitySlug.js.map