"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var unstructured_introspection_exports = {};
__export(unstructured_introspection_exports, {
  unstructuredIntrospection: () => unstructuredIntrospection
});
module.exports = __toCommonJS(unstructured_introspection_exports);
var import_js_base64 = require("js-base64");
var import_zod = require("zod");
var import_api = require("../../api");
function isAPIResourceType(val) {
  return import_api.APIResourceType[val] != null;
}
const parseResourceIdSchema = import_zod.z.string().transform((val, ctx) => {
  try {
    const match = import_js_base64.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: import_zod.z.ZodIssueCode.custom,
      message: `${String(val)} is not a valid node resource ID`
    });
    return import_zod.z.NEVER;
  }
});
function unstructuredIntrospection(element) {
  const typeIdsToFound = /* @__PURE__ */ new Map();
  Object.values(import_api.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(import_api.APIResourceType.Swatch) ?? /* @__PURE__ */ new Set(),
    typographyIds: typeIdsToFound.get(import_api.APIResourceType.Typography) ?? /* @__PURE__ */ new Set(),
    fileIds: typeIdsToFound.get(import_api.APIResourceType.File) ?? /* @__PURE__ */ new Set(),
    tableIds: typeIdsToFound.get(import_api.APIResourceType.Table) ?? /* @__PURE__ */ new Set(),
    pageIds: typeIdsToFound.get(import_api.APIResourceType.Page) ?? /* @__PURE__ */ new Set(),
    globalElementIds: typeIdsToFound.get(import_api.APIResourceType.GlobalElement) ?? /* @__PURE__ */ new Set()
  };
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  unstructuredIntrospection
});
//# sourceMappingURL=unstructured-introspection.js.map