// src/platform/core/figma-helpers.ts
var DIRECT_NODE_ID_PATTERN = /^\d+[:-]\d+$/;
var FIGMA_QUERY_NODE_ID_KEYS = ["node-id", "nodeId"];
var SUPPORTED_FIGMA_ROUTE_PREFIXES = /* @__PURE__ */ new Set(["design", "file", "proto"]);
function normalizeFigmaNodeId(nodeId) {
  return decodeURIComponent(nodeId).trim().replace(/-/g, ":");
}
function readFigmaNodeId(url) {
  for (const key of FIGMA_QUERY_NODE_ID_KEYS) {
    const queryValue = url.searchParams.get(key);
    if (queryValue) {
      return queryValue;
    }
  }
  const hashParams = new URLSearchParams(url.hash.startsWith("#") ? url.hash.slice(1) : url.hash);
  for (const key of FIGMA_QUERY_NODE_ID_KEYS) {
    const hashValue = hashParams.get(key);
    if (hashValue) {
      return hashValue;
    }
  }
  return "";
}
function readFigmaFileKeys(url) {
  const segments = url.pathname.split("/").filter(Boolean);
  const routePrefix = segments[0];
  if (!routePrefix || !SUPPORTED_FIGMA_ROUTE_PREFIXES.has(routePrefix) || !segments[1]) {
    return {
      branchKey: "",
      fileKey: "",
      resolvedFileKey: ""
    };
  }
  const fileKey = segments[1];
  const branchKey = segments[2] === "branch" && segments[3] ? segments[3] : "";
  return {
    branchKey,
    fileKey,
    resolvedFileKey: branchKey || fileKey
  };
}
function parseFigmaAddress(sourceAddress) {
  const originalAddress = sourceAddress.trim();
  const parsed = {
    originalAddress,
    parseNotes: []
  };
  if (!originalAddress) {
    parsed.parseNotes.push("\uC785\uB825\uAC12\uC774 \uBE44\uC5B4 \uC788\uC5B4 Figma \uC8FC\uC18C\uB97C \uD30C\uC2F1\uD558\uC9C0 \uBABB\uD588\uC2B5\uB2C8\uB2E4.");
    return parsed;
  }
  if (DIRECT_NODE_ID_PATTERN.test(originalAddress)) {
    parsed.nodeId = originalAddress;
    parsed.normalizedNodeId = normalizeFigmaNodeId(originalAddress);
    parsed.parseNotes.push("URL \uB300\uC2E0 node id \uC9C1\uC811 \uC785\uB825\uC73C\uB85C \uD574\uC11D\uD588\uC2B5\uB2C8\uB2E4.");
    return parsed;
  }
  let url;
  try {
    url = new URL(originalAddress);
  } catch {
    parsed.parseNotes.push("\uC720\uD6A8\uD55C URL\uB85C \uD574\uC11D\uB418\uC9C0 \uC54A\uC544 \uC6D0\uBCF8 \uBB38\uC790\uC5F4\uB9CC \uC0AC\uC6A9\uD569\uB2C8\uB2E4.");
    return parsed;
  }
  const { branchKey, fileKey, resolvedFileKey } = readFigmaFileKeys(url);
  const nodeId = readFigmaNodeId(url);
  parsed.branchKey = branchKey || void 0;
  parsed.fileKey = fileKey || void 0;
  parsed.resolvedFileKey = resolvedFileKey || void 0;
  parsed.nodeId = nodeId || void 0;
  parsed.normalizedNodeId = nodeId ? normalizeFigmaNodeId(nodeId) : void 0;
  if (!fileKey) {
    parsed.parseNotes.push("Figma URL path\uC5D0\uC11C file key\uB97C \uCD94\uCD9C\uD558\uC9C0 \uBABB\uD588\uC2B5\uB2C8\uB2E4.");
  }
  if (!nodeId) {
    parsed.parseNotes.push("Figma URL\uC5D0\uC11C node-id query\uB97C \uCC3E\uC9C0 \uBABB\uD588\uC2B5\uB2C8\uB2E4.");
  }
  if (branchKey) {
    parsed.parseNotes.push("branch URL\uC774\uBBC0\uB85C branch key\uB97C MCP file key\uCC98\uB7FC \uC0AC\uC6A9\uD560 \uC218 \uC788\uC2B5\uB2C8\uB2E4.");
  }
  return parsed;
}

export { normalizeFigmaNodeId, parseFigmaAddress };
