import { Base64 } from "js-base64";
import { z } from "zod";
import { APIResourceType } from "../../api";
function isAPIResourceType(val) {
  return APIResourceType[val] != null;
}
const parseResourceIdSchema = z.string().transform((val, ctx) => {
  try {
    const match = Base64.decode(val).match(/^([^:]+):(.+)$/);
    if (!match)
      throw new TypeError(`NodeID cannot represent value: ${String(val)}`);
    const [, typeName, key] = match;
    if (isAPIResourceType(typeName)) {
      return { key, typeName };
    }
    throw new TypeError(`decoded type '${typeName}' is not a valid APIResourceType`);
  } catch (error) {
    ctx.addIssue({
      code: z.ZodIssueCode.custom,
      message: `${String(val)} is not a valid node resource ID`
    });
    return z.NEVER;
  }
});
function unstructuredIntrospection(element) {
  const typeIdsToFound = /* @__PURE__ */ new Map();
  Object.values(APIResourceType).forEach((resourceType) => {
    typeIdsToFound.set(resourceType, /* @__PURE__ */ new Set());
  });
  function findResourceIds(curr) {
    if (typeof curr === "string") {
      const parseResourceResult = parseResourceIdSchema.safeParse(curr);
      if (parseResourceResult.success) {
        typeIdsToFound.get(parseResourceResult.data.typeName)?.add(curr);
      }
    } else if (Array.isArray(curr)) {
      for (const item of curr) {
        findResourceIds(item);
      }
    } else if (curr != null && typeof curr === "object") {
      for (const key in curr) {
        if (Object.prototype.hasOwnProperty.call(curr, key)) {
          findResourceIds(curr[key]);
        }
      }
    }
  }
  findResourceIds(element);
  return {
    swatchIds: typeIdsToFound.get(APIResourceType.Swatch) ?? /* @__PURE__ */ new Set(),
    typographyIds: typeIdsToFound.get(APIResourceType.Typography) ?? /* @__PURE__ */ new Set(),
    fileIds: typeIdsToFound.get(APIResourceType.File) ?? /* @__PURE__ */ new Set(),
    tableIds: typeIdsToFound.get(APIResourceType.Table) ?? /* @__PURE__ */ new Set(),
    pageIds: typeIdsToFound.get(APIResourceType.Page) ?? /* @__PURE__ */ new Set(),
    globalElementIds: typeIdsToFound.get(APIResourceType.GlobalElement) ?? /* @__PURE__ */ new Set()
  };
}
export {
  unstructuredIntrospection
};
//# sourceMappingURL=unstructured-introspection.js.map