"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.generateTestIds = generateTestIds;
/**
 * Generates list of test Ids for E2E tests.
 */
function generateTestIds(testIdCollector) {
    // TestIdCollector collects all Ids during the generation process. We flush them here to get all Ids
    const collectedIds = testIdCollector.flush();
    const object = {};
    const ids = collectedIds;
    ids.sort((a, b) => a.localeCompare(b));
    for (const id of ids) {
        const keys = id.split('-');
        extendObject(object, keys, id);
    }
    const testIds = JSON.stringify(object, null, 2);
    return /* ts */ `
  /**
   * Object with all generated test ids.
   */
  export const MODEL_TEST_IDS =
  ${testIds}
  `;
}
/**
 * Recursively traverses the keys and adds the value to the object in a nested way.
 * E.g. `{object, keys: ['post', 'create', 'name'], value}` will extend `object` with `{post: {create: {name: value}}}`.
 *
 */
function extendObject(object, keys, value) {
    const key = keys.shift();
    // no item left -> we are at the end of the recursion
    if (!key) {
        return;
    }
    // create the nested object if it does not exist yet
    object[key] = object[key] || {};
    // In case the object already has a value, we check if it is a string (and not a nested object).
    // If it is a string, we check if the value is the same as the new value. In this case it is a duplicate and hence fine.
    // However, if it is a string and a different value, it means we have an inconsistent ID structure.
    // E.g. this can happen when we have three entries: "post-create", "post-create-name", "post-create-description".
    // In this case, the "post-create" entry will be a string - and we cannot nest `{name: ..., description: ...}` in it.
    if (key in object && typeof object[key] === 'string') {
        if (keys.length !== 0) {
            throw new Error(`Key ${key} already exists with value: ${object[key]} but we are trying to nest ${keys.join('-')}`);
        }
        if (object[key] !== value) {
            throw new Error(`Duplicate key ${key} with different values: ${object[key]} and ${value}`);
        }
        return;
    }
    // If there are still keys left, we recursively call this function with the nested object and the remaining keys.
    if (keys.length !== 0) {
        extendObject(object[key], keys, value);
        return;
    }
    // If there are no keys left, we need to verify that the object might not already have a nested object.
    // This can happen, e.g. when we have two entries: "post-create-name" and "post-create".
    // In this case, the `post.create` entry will be a nested object`{name: ...}`.
    // If we would set `post.create` to a string, we would lose the nested object.
    if (key in object && typeof object[key] !== 'string' && Object.keys(object[key]).length !== 0) {
        throw new Error(`Key ${key} already has nested object ${JSON.stringify(object[key])} but we are trying to set it to ${value}`);
    }
    object[key] = value;
}
//# sourceMappingURL=test-id-generator.js.map