function getNestedValue(obj, path) {
  return path.split(".").reduce((current, key) => {
    if (current && typeof current === "object" && key in current) {
      return current[key];
    }
    return void 0;
  }, obj);
}
function replacePlaceholders(selector, replacements) {
  if (!replacements) return selector;
  return Object.entries(replacements).reduce(
    (result, [key, value]) => result.replace(new RegExp(`\\{${key}\\}`, "g"), String(value)),
    selector
  );
}
const isDevelopment = process.env.NODE_ENV === "development";
const isTest = process.env.NODE_ENV === "test";
const enableTestingAttributes = isDevelopment || isTest;
function createSelectorHelpers(selectors) {
  function sel(path, replacements) {
    const value = getNestedValue(selectors, path);
    if (value === void 0) {
      if (process.env.NODE_ENV === "development") {
        console.error(`[sel] Invalid selector path: "${path}"`);
      }
      return `INVALID_SELECTOR_${path.replace(/\./g, "_")}`;
    }
    if (typeof value !== "string") {
      if (process.env.NODE_ENV === "development") {
        console.error(
          `[sel] Path "${path}" points to an object, not a string. Did you forget a property?`
        );
      }
      return `INVALID_SELECTOR_${path.replace(/\./g, "_")}`;
    }
    return replacePlaceholders(value, replacements);
  }
  function cySelector(path, replacements) {
    return `[data-cy="${sel(path, replacements)}"]`;
  }
  function selDev(path, replacements) {
    if (!enableTestingAttributes) return void 0;
    return sel(path, replacements);
  }
  function entitySelectors(slug) {
    return {
      // Page
      page: () => sel("entities.page.container", { slug }),
      title: () => sel("entities.page.title", { slug }),
      // Table
      table: () => sel("entities.table.element", { slug }),
      tableContainer: () => sel("entities.table.container", { slug }),
      search: () => sel("entities.table.search", { slug }),
      addButton: () => sel("entities.table.addButton", { slug }),
      row: (id) => sel("entities.table.row", { slug, id }),
      rowMenu: (id) => sel("entities.table.rowMenu", { slug, id }),
      rowAction: (action, id) => sel("entities.table.rowAction", { slug, action, id }),
      cell: (field, id) => sel("entities.table.cell", { slug, field, id }),
      // Form
      form: () => sel("entities.form.container", { slug }),
      field: (name) => sel("entities.form.field", { slug, name }),
      submitButton: () => sel("entities.form.submitButton", { slug }),
      // Header
      header: (mode) => sel("entities.header.container", { slug, mode }),
      backButton: () => sel("entities.header.backButton", { slug }),
      editButton: () => sel("entities.header.editButton", { slug }),
      deleteButton: () => sel("entities.header.deleteButton", { slug }),
      // Detail
      detail: () => sel("entities.detail.container", { slug }),
      // Filter
      filter: (field) => sel("entities.filter.container", { slug, field }),
      filterTrigger: (field) => sel("entities.filter.trigger", { slug, field }),
      filterOption: (field, value) => sel("entities.filter.option", { slug, field, value })
    };
  }
  return {
    SELECTORS: selectors,
    sel,
    s: sel,
    selDev,
    cySelector,
    entitySelectors
  };
}
export {
  createSelectorHelpers,
  getNestedValue,
  replacePlaceholders
};
