"use strict";
/* Socket Lib - Built with esbuild */
"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 validate_exports = {};
__export(validate_exports, {
  isTypeBoxSchema: () => isTypeBoxSchema,
  normalizeTypeBoxErrors: () => normalizeTypeBoxErrors,
  normalizeZodError: () => normalizeZodError,
  validateSchema: () => validateSchema
});
module.exports = __toCommonJS(validate_exports);
var import_array = require("../primordials/array");
var import_error = require("../primordials/error");
var import_number = require("../primordials/number");
var import_object = require("../primordials/object");
function isTypeBoxSchema(schema) {
  if (schema === null || typeof schema !== "object") {
    return false;
  }
  for (const sym of (0, import_object.ObjectGetOwnPropertySymbols)(schema)) {
    if (sym.description === "TypeBox.Kind") {
      return typeof schema[sym] === "string";
    }
  }
  return false;
}
function normalizeTypeBoxErrors(errors) {
  const out = [];
  for (const err of errors) {
    const segs = err.path.split("/").filter(Boolean);
    out.push({
      path: segs.map((s) => {
        const n = Number(s);
        return (0, import_number.NumberIsInteger)(n) && String(n) === s ? n : s;
      }),
      message: err.message
    });
  }
  return out;
}
function normalizeZodError(err) {
  if (err === null || typeof err !== "object") {
    return [{ path: [], message: String(err) }];
  }
  const issues = err.issues;
  if (!(0, import_array.ArrayIsArray)(issues)) {
    return [{ path: [], message: "Unknown validation error" }];
  }
  return issues.map((issue) => {
    const i = issue;
    return {
      path: (0, import_array.ArrayIsArray)(i.path) ? i.path : [],
      message: typeof i.message === "string" ? i.message : "Invalid value"
    };
  });
}
function validateSchema(schema, data) {
  if (isTypeBoxSchema(schema)) {
    const { Value } = require("../external/@sinclair/typebox/value");
    if (Value.Check(schema, data)) {
      return { ok: true, value: data };
    }
    return {
      ok: false,
      errors: normalizeTypeBoxErrors(Value.Errors(schema, data))
    };
  }
  if (schema !== null && typeof schema === "object" && typeof schema.safeParse === "function") {
    const result = schema.safeParse(data);
    if (result.success === true) {
      return {
        ok: true,
        value: result.data
      };
    }
    return {
      ok: false,
      errors: normalizeZodError(result.error)
    };
  }
  throw new import_error.TypeErrorCtor(
    "validateSchema: unsupported schema kind. Expected a Zod schema or an object with a safeParse method."
  );
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  isTypeBoxSchema,
  normalizeTypeBoxErrors,
  normalizeZodError,
  validateSchema
});
