{"version":3,"file":"transformSanityNodeData.cjs","sources":["../../../visual-editing-helpers/dist/_chunks-es/transformSanityNodeData.js"],"sourcesContent":["import { getPublishedId, studioPath } from \"@sanity/client/csm\";\nimport { pipe, string, minLength, optional, object, record, unknown, is, safeParse, parse } from \"valibot\";\nfunction isRecord(value) {\n  return typeof value == \"object\" && value !== null && !Array.isArray(value);\n}\nfunction isArray(value) {\n  return value !== null && Array.isArray(value);\n}\nfunction pathToUrlString(path) {\n  let str = \"\";\n  for (const segment of path) {\n    if (typeof segment == \"string\") {\n      str && (str += \".\"), str += segment;\n      continue;\n    }\n    if (typeof segment == \"number\") {\n      str && (str += \":\"), str += `${segment}`;\n      continue;\n    }\n    if (isArray(segment)) {\n      str && (str += \":\"), str += `${segment.join(\",\")}}`;\n      continue;\n    }\n    if (segment._key) {\n      str && (str += \":\"), str += `${segment._key}`;\n      continue;\n    }\n  }\n  return str;\n}\nconst RE_SEGMENT_WITH_INDEX = /^([\\w-]+):(0|[1-9][0-9]*)$/, RE_SEGMENT_WITH_TUPLE = /^([\\w-]+):([0-9]+),([0-9]+)$/, RE_SEGMENT_WITH_KEY = /^([\\w-]+):([\\w-]+)$/;\nfunction urlStringToPath(str) {\n  const path = [];\n  for (const segment of str.split(\".\")) {\n    const withIndex = RE_SEGMENT_WITH_INDEX.exec(segment);\n    if (withIndex) {\n      path.push(withIndex[1], Number(withIndex[2]));\n      continue;\n    }\n    const withTuple = RE_SEGMENT_WITH_TUPLE.exec(segment);\n    if (withTuple) {\n      path.push(withTuple[1], [Number(withTuple[2]), Number(withTuple[3])]);\n      continue;\n    }\n    const withKey = RE_SEGMENT_WITH_KEY.exec(segment);\n    if (withKey) {\n      path.push(withKey[1], { _key: withKey[2] });\n      continue;\n    }\n    path.push(segment);\n  }\n  return path;\n}\nconst DRAFTS_PREFIX = \"drafts.\", lengthyStr = pipe(string(), minLength(1)), optionalLengthyStr = optional(lengthyStr), sanityNodeSchema = object({\n  baseUrl: lengthyStr,\n  dataset: optionalLengthyStr,\n  id: lengthyStr,\n  path: lengthyStr,\n  projectId: optionalLengthyStr,\n  tool: optionalLengthyStr,\n  type: optionalLengthyStr,\n  workspace: optionalLengthyStr,\n  isDraft: optional(string())\n}), sanityLegacyNodeSchema = object({\n  origin: lengthyStr,\n  href: lengthyStr,\n  data: optional(record(string(), unknown()))\n});\nfunction isValidSanityNode(node) {\n  return is(sanityNodeSchema, node);\n}\nfunction isValidSanityLegacyNode(node) {\n  return is(sanityLegacyNodeSchema, node);\n}\nfunction encodeSanityNodeData(node) {\n  const { id: _id, path, baseUrl, tool, workspace, type } = node;\n  return isValidSanityNode(node) ? [\n    [\"id\", getPublishedId(_id)],\n    [\"type\", type],\n    [\"path\", pathToUrlString(studioPath.fromString(path))],\n    [\"base\", encodeURIComponent(baseUrl)],\n    [\"workspace\", workspace],\n    [\"tool\", tool],\n    [\"isDraft\", _id.startsWith(DRAFTS_PREFIX)]\n  ].filter(([, value]) => !!value).map((part) => {\n    const [key, value] = part;\n    return value === !0 ? key : part.join(\"=\");\n  }).join(\";\") : void 0;\n}\nfunction decodeSanityString(str) {\n  const data = str.split(\";\").reduce((acc, segment) => {\n    const [key, value] = segment.split(\"=\");\n    if (!key || segment.includes(\"=\") && !value) return acc;\n    switch (key) {\n      case \"id\":\n        acc.id = value;\n        break;\n      case \"type\":\n        acc.type = value;\n        break;\n      case \"path\":\n        acc.path = studioPath.toString(urlStringToPath(value));\n        break;\n      case \"base\":\n        acc.baseUrl = decodeURIComponent(value);\n        break;\n      case \"tool\":\n        acc.tool = value;\n        break;\n      case \"workspace\":\n        acc.workspace = value;\n        break;\n      case \"projectId\":\n        acc.projectId = value;\n        break;\n      case \"dataset\":\n        acc.dataset = value;\n        break;\n      case \"isDraft\":\n        acc.isDraft = \"\";\n        break;\n    }\n    return acc;\n  }, {});\n  if (isValidSanityNode(data))\n    return data;\n}\nfunction decodeSanityObject(data) {\n  const sanityNode = safeParse(sanityNodeSchema, data);\n  if (sanityNode.success)\n    return sanityNode.output;\n  const sanityLegacyNode = safeParse(sanityLegacyNodeSchema, data);\n  if (sanityLegacyNode.success)\n    try {\n      const url = new URL(\n        sanityLegacyNode.output.href,\n        typeof document > \"u\" ? \"https://example.com\" : location.origin\n      );\n      return url.searchParams.size > 0 ? parse(sanityNodeSchema, Object.fromEntries(url.searchParams.entries())) : sanityLegacyNode.output;\n    } catch (err) {\n      return console.error(\"Failed to parse sanity node\", err), sanityLegacyNode.output;\n    }\n}\nfunction decodeSanityNodeData(data) {\n  if (typeof data == \"object\" && data !== null)\n    return decodeSanityObject(data);\n  try {\n    const obj = JSON.parse(data);\n    return decodeSanityObject(obj);\n  } catch {\n    return decodeSanityString(data);\n  }\n}\nexport {\n  DRAFTS_PREFIX,\n  decodeSanityNodeData,\n  decodeSanityString,\n  encodeSanityNodeData,\n  isArray,\n  isRecord,\n  isValidSanityLegacyNode,\n  isValidSanityNode,\n  pathToUrlString,\n  urlStringToPath\n};\n//# sourceMappingURL=transformSanityNodeData.js.map\n"],"names":["value","Array","isArray"],"mappings":"oJAImB,QADKA,MACGC,MAAMC,QAAQF,sHADlC,IAAiBA"}