import { toRaw } from "vue";
export const ROW_ID_SYMBOL = Symbol.for("shwfed.table.row-id");
export function readSymbolId(row) {
  const id = toRaw(row)[ROW_ID_SYMBOL];
  return typeof id === "string" ? id : void 0;
}
export function stampSymbolId(row, id) {
  const next = id ?? crypto.randomUUID();
  toRaw(row)[ROW_ID_SYMBOL] = next;
  return next;
}
export function carrySymbolId(prev, next) {
  if (!prev || typeof prev !== "object") return;
  if (!next || typeof next !== "object") return;
  const prevId = readSymbolId(prev);
  if (!prevId) return;
  if (readSymbolId(next) !== void 0) return;
  stampSymbolId(next, prevId);
}
export function createRowKeyResolver(options) {
  return (row, index) => {
    if (row === null || typeof row !== "object") return `__primitive_${index}`;
    const obj = row;
    const keyed = options.evaluateKey(obj, index);
    if (keyed !== void 0 && keyed !== null && keyed !== "") {
      return `k:${String(keyed)}`;
    }
    return readSymbolId(obj) ?? stampSymbolId(obj);
  };
}
