import { getSlugMaxLength } from "../../../items/_internal/slugs";
import { cloneObject } from "../../../util";
/**
 * add slug max length and unique validation to schema
 * based on the orgUrlKey
 * @param schema
 * @param options
 * @returns
 */
export const addDynamicSlugValidation = (schema, options) => {
    var _a;
    const _slug = cloneObject((_a = schema.properties) === null || _a === void 0 ? void 0 : _a._slug);
    if (!_slug) {
        return schema;
    }
    // add max length to slug
    let { orgUrlKey } = options;
    // orgUrlKey is undefined in Enterprise
    if (!orgUrlKey) {
        orgUrlKey = "";
    }
    // ensure orgUrlKey is lowercase
    orgUrlKey = orgUrlKey.toLowerCase();
    _slug.maxLength = getSlugMaxLength(orgUrlKey);
    // add conditional validation to ensure slug is unique
    const allOf = cloneObject(schema.allOf) || [];
    const { pattern } = _slug;
    // remove pattern from base schema so the form doesn't always apply the pattern
    // validation. We will add it back to the allOf conditionally
    delete _slug.pattern;
    const { id } = options;
    allOf.push({
        // only enforce the pattern if slug has been entered
        if: { properties: { _slug: { minLength: 1 } } },
        then: { properties: { _slug: { pattern } } },
    }, {
        // only do async isUniqueSlug check if the slug is valid
        if: { properties: { _slug: { pattern } } },
        then: {
            // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
            properties: { _slug: { isUniqueSlug: { id, orgUrlKey } } },
        },
    });
    const clone = cloneObject(schema);
    clone.properties._slug = _slug;
    clone.allOf = allOf;
    return clone;
};
//# sourceMappingURL=addDynamicSlugValidation.js.map