function isDict(value) {
  return Boolean(value) && typeof value === "object" && !Array.isArray(value);
}
function normalizeString(value) {
  if (typeof value !== "string") return "";
  return value.trim();
}
function normalizeAttachmentTags(input) {
  if (!input) return [];
  const values = Array.isArray(input) ? input.map((entry) => normalizeString(entry)) : typeof input === "string" ? input.split(",").map((entry) => normalizeString(entry)) : [];
  const unique = new Set(values.filter((entry) => entry.length > 0));
  return Array.from(unique.values());
}
function normalizeAssignment(entry) {
  if (!isDict(entry)) return null;
  const type = normalizeString(entry.type);
  const id = normalizeString(entry.id);
  if (!type || !id) return null;
  const assignment = { type, id };
  const href = normalizeString(entry.href);
  if (href) assignment.href = href;
  else if (entry.href === null) assignment.href = null;
  const label = normalizeString(entry.label);
  if (label) assignment.label = label;
  return assignment;
}
function normalizeAttachmentAssignments(input) {
  if (!input) return [];
  const entries = Array.isArray(input) ? input : isDict(input) ? [input] : [];
  const map = /* @__PURE__ */ new Map();
  for (const entry of entries) {
    const normalized = normalizeAssignment(entry);
    if (!normalized) continue;
    const key = `${normalized.type}:${normalized.id}`;
    map.set(key, normalized);
  }
  return Array.from(map.values());
}
function readAttachmentMetadata(raw) {
  const base = isDict(raw) ? { ...raw } : {};
  const assignments = normalizeAttachmentAssignments(base.assignments);
  const tags = normalizeAttachmentTags(base.tags);
  return {
    ...base,
    assignments,
    tags
  };
}
function mergeAttachmentMetadata(raw, patch) {
  const base = readAttachmentMetadata(raw);
  if (patch.assignments) base.assignments = normalizeAttachmentAssignments(patch.assignments);
  if (patch.tags) base.tags = normalizeAttachmentTags(patch.tags);
  return base;
}
function upsertAssignment(assignments, entry) {
  if (!entry) return assignments;
  const key = `${entry.type}:${entry.id}`;
  const map = new Map(
    assignments.map((candidate) => [`${candidate.type}:${candidate.id}`, candidate])
  );
  map.set(key, entry);
  return Array.from(map.values());
}
export {
  mergeAttachmentMetadata,
  normalizeAttachmentAssignments,
  normalizeAttachmentTags,
  readAttachmentMetadata,
  upsertAssignment
};
//# sourceMappingURL=metadata.js.map
