"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/utils/rtlUtils.tsx
var rtlUtils_exports = {};
__export(rtlUtils_exports, {
  blur: () => import_rtl_utils.blur,
  cell: () => cell,
  cellAnd: () => cellAnd,
  cellOf: () => cellOf,
  change: () => import_rtl_utils.change,
  click: () => import_rtl_utils.click,
  clickAndWait: () => import_rtl_utils.clickAndWait,
  focus: () => import_rtl_utils.focus,
  getOptions: () => getOptions,
  getSelected: () => getSelected,
  input: () => import_rtl_utils.input,
  row: () => row,
  rowAnd: () => rowAnd,
  select: () => select,
  selectAndWait: () => selectAndWait,
  tableSnapshot: () => tableSnapshot,
  type: () => import_rtl_utils.type,
  typeAndWait: () => import_rtl_utils.typeAndWait,
  wait: () => import_rtl_utils.wait
});
module.exports = __toCommonJS(rtlUtils_exports);
var import_rtl_utils = require("@homebound/rtl-utils");
var import_react = require("@testing-library/react");

// src/utils/defaultTestId.ts
var import_change_case = require("change-case");

// src/utils/index.ts
function fail(message) {
  throw new Error(message || "Failed");
}

// src/utils/rtlUtils.tsx
function cell(r, row2, column) {
  return cellOf(r, "gridTable", row2, column);
}
function cellOf(r, tableTestId, rowNum, column) {
  return row(r, rowNum, tableTestId).childNodes[column];
}
function cellAnd(r, row2, column, testId) {
  return cell(r, row2, column).querySelector(`[data-testid="${testId}"]`) || fail(`Element not found ${(0, import_react.prettyDOM)(cell(r, row2, column))}`);
}
function row(r, row2, tableTestId = "gridTable") {
  const dataRows = Array.from(r.getByTestId(tableTestId).querySelectorAll("[data-gridrow]"));
  return dataRows[row2];
}
function rowAnd(r, rowNum, testId) {
  const e = row(r, rowNum);
  return e.querySelector(`[data-testid="${testId}"]`) || fail(`Element not found ${(0, import_react.prettyDOM)(e)}`);
}
function tableSnapshot(r, columnNames = [], tableTestId = "gridTable") {
  const tableEl = r.getByTestId(tableTestId);
  const dataRows = Array.from(tableEl.querySelectorAll("[data-gridrow]"));
  const hasExpandableHeader = !!tableEl.querySelector(`[data-testid="expandableColumn"]`);
  let tableDataAsStrings = dataRows.map((row2) => {
    return Array.from(row2.childNodes).map(getTextFromTableCellNode);
  });
  if (columnNames.length > 0) {
    const headerCells = tableDataAsStrings[0];
    if (headerCells) {
      const columnIndices = columnNames.map((name) => {
        const i = headerCells.indexOf(name);
        if (i === -1) throw new Error(`Could not find header '${name}' in ${headerCells.join(", ")}`);
        return i;
      });
      tableDataAsStrings = tableDataAsStrings.map((row2) => columnIndices.map((index) => row2[index]));
    }
  }
  return toMarkupTableString(tableDataAsStrings, hasExpandableHeader);
}
function toMarkupTableString(tableRows, hasExpandableHeader) {
  const columnWidths = tableRows.reduce((acc, row2) => {
    row2.forEach((cell2, columnIndex) => {
      const cellWidth = cell2?.length ?? 0;
      const currentMaxWidth = acc.get(columnIndex) ?? 0;
      if (cellWidth > currentMaxWidth || !currentMaxWidth) acc.set(columnIndex, cellWidth);
    });
    return acc;
  }, /* @__PURE__ */ new Map());
  const wrapTableRowEnds = (str) => `| ${str} |`;
  const rowsWithPaddingAndDividers = tableRows.map((tableCells) => {
    const formattedRow = tableCells.map((cell2, columnIndex) => {
      const cellWidth = columnWidths.get(columnIndex) ?? 0;
      return cell2?.padEnd(cellWidth, " ") || "";
    }).join(" | ");
    return wrapTableRowEnds(formattedRow);
  });
  const headerDivider = Array.from(columnWidths.values()).map((width) => "-".repeat(width) ?? "").join(" | ");
  const headerDividerRowNumber = hasExpandableHeader ? 2 : 1;
  rowsWithPaddingAndDividers.splice(headerDividerRowNumber, 0, wrapTableRowEnds(headerDivider));
  return `
${rowsWithPaddingAndDividers.join("\n")}
`;
}
function getTextFromTableCellNode(node) {
  if (node.nodeType === Node.ELEMENT_NODE) {
    const element = node;
    const maybeInput = element.getElementsByTagName("input")[0];
    if (maybeInput) return maybeInput.value;
    const maybeTextarea = element.getElementsByTagName("textarea")[0];
    if (maybeTextarea) return maybeTextarea.value;
    const maybeSelect = element.getElementsByTagName("select")[0];
    if (maybeSelect) return maybeSelect.value;
  }
  return node.textContent;
}
function select(select2, value) {
  assertListBoxInput(select2);
  ensureListBoxOpen(select2);
  const optionValues = Array.isArray(value) ? value : [value];
  optionValues.forEach((optionValue) => selectOption(select2, optionValue));
}
async function selectAndWait(select2, value) {
  await (0, import_rtl_utils.allowAndWaitForAsyncBehavior)(() => ensureListBoxOpen(select2));
  return (0, import_rtl_utils.allowAndWaitForAsyncBehavior)(() => {
    const optionValues = Array.isArray(value) ? value : [value];
    optionValues.forEach((optionValue) => selectOption(select2, optionValue));
  });
}
function ensureListBoxOpen(select2) {
  const expanded = select2.getAttribute("aria-expanded") === "true";
  if (!expanded) {
    (0, import_rtl_utils.click)(select2);
  }
}
function selectOption(select2, optionValue) {
  const listbox = findListBox(select2);
  const options = listbox.querySelectorAll("[role=option]");
  const optionToSelect = Array.from(options).find(
    (o) => o.dataset.key === optionValue || o.dataset.label === optionValue
  );
  if (!optionToSelect) {
    throw new Error(`Could not find option with value or text content of ${optionValue}`);
  }
  if (optionToSelect.getAttribute("aria-disabled")) {
    throw new Error(`Cannot select disabled option ${optionValue}`);
  }
  (0, import_rtl_utils.click)(optionToSelect);
}
function getSelected(select2) {
  if (isSelectElement(select2)) {
    throw new Error("Beam getSelected helper does not support <select> elements");
  }
  if (!isInputOrTextAreaElement(select2) && select2.dataset.readonly === "true") {
    return select2.textContent ?? void 0;
  }
  ensureListBoxOpen(select2);
  const listbox = findListBox(select2);
  const options = listbox.querySelectorAll("[role=option]");
  const selections = Array.from(options).filter((o) => o.getAttribute("aria-selected") === "true").map((o) => o.dataset.label ?? o.dataset.key ?? "").filter((o) => !!o);
  return selections.length > 0 ? selections.length > 1 ? selections : selections[0] : void 0;
}
function getOptions(select2) {
  assertListBoxInput(select2);
  ensureListBoxOpen(select2);
  const listbox = findListBox(select2);
  const options = listbox.querySelectorAll("[role=option]");
  return Array.from(options).map((o) => o.dataset.label ?? o.dataset.key ?? "").filter((o) => !!o);
}
function findListBox(select2) {
  if (select2.tagName === "DIV") fail("SelectField is readOnly");
  const listboxId = select2.getAttribute("aria-controls") || fail("aria-controls attribute not found, the SelectField is probably readOnly");
  return document.getElementById(listboxId) || fail("listbox not found");
}
function assertListBoxInput(select2) {
  if (isSelectElement(select2)) {
    throw new Error("Beam getOptions helper does not support <select> elements");
  }
  if (!isInputOrTextAreaElement(select2)) {
    throw new Error(
      `Expected element to be INPUT or TEXTAREA, but got ${select2.nodeName}. This field may be read-only. In that case we cannot get the list of options`
    );
  }
  return true;
}
function isSelectElement(element) {
  return element.nodeName === "SELECT";
}
function isInputOrTextAreaElement(element) {
  return element.nodeName === "INPUT" || element.nodeName === "TEXTAREA";
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  blur,
  cell,
  cellAnd,
  cellOf,
  change,
  click,
  clickAndWait,
  focus,
  getOptions,
  getSelected,
  input,
  row,
  rowAnd,
  select,
  selectAndWait,
  tableSnapshot,
  type,
  typeAndWait,
  wait
});
//# sourceMappingURL=rtlUtils.cjs.map