import { createLogger } from "../../lib/logger/index.js";
const logger = createLogger("widgets").child({ component: "injection-placement" });
var InjectionPosition = /* @__PURE__ */ ((InjectionPosition2) => {
  InjectionPosition2["Before"] = "before";
  InjectionPosition2["After"] = "after";
  InjectionPosition2["First"] = "first";
  InjectionPosition2["Last"] = "last";
  return InjectionPosition2;
})(InjectionPosition || {});
function getInjectionPosition(placement) {
  if (!placement?.position) return "last" /* Last */;
  return placement.position;
}
function warnInvalidRelativeTo(relativeTo) {
  if (process.env.NODE_ENV !== "development") return;
  if (!relativeTo) return;
  logger.warn("relativeTo target not found, appending item at the end", { relativeTo });
}
function insertByInjectionPlacement(items, item, placement, getItemId) {
  const current = [...items];
  const position = getInjectionPosition(placement);
  if (position === "first" /* First */) {
    current.unshift(item);
    return current;
  }
  if (position === "last" /* Last */) {
    current.push(item);
    return current;
  }
  const relativeTo = placement?.relativeTo;
  if (!relativeTo) {
    current.push(item);
    return current;
  }
  const targetIndex = current.findIndex((value) => getItemId(value) === relativeTo);
  if (targetIndex < 0) {
    warnInvalidRelativeTo(relativeTo);
    current.push(item);
    return current;
  }
  const insertIndex = position === "before" /* Before */ ? targetIndex : targetIndex + 1;
  current.splice(insertIndex, 0, item);
  return current;
}
export {
  InjectionPosition,
  getInjectionPosition,
  insertByInjectionPlacement
};
//# sourceMappingURL=injection-position.js.map
