import {
  Schema,
  parseWithError
} from "./chunk-MGSKOQTQ.js";
import {
  formatValidationErrors
} from "./chunk-PB3KY5ZL.js";
import {
  BadRequestError
} from "./chunk-YCLRZPR6.js";
import {
  init_cjs_shims
} from "./chunk-7GH3YGSC.js";

// src/utils/schemaValidation.ts
init_cjs_shims();
var validateSchemaStructure = (schema) => {
  try {
    parseWithError(
      Schema,
      schema,
      BadRequestError,
      {
        message: `Invalid schema provided`
      }
    );
  } catch (e) {
    const { errors } = formatValidationErrors(e);
    const error = new Error("Schema validation failed");
    error.validationErrors = errors;
    throw error;
  }
};
var countAccountsRecursively = (accounts) => {
  let count = accounts.length;
  for (const account of accounts) {
    if (account.children) {
      count += countAccountsRecursively(account.children);
    }
  }
  return count;
};
var extractSchemaMetadata = (schema) => {
  const accountCount = countAccountsRecursively(schema.chartOfAccounts?.accounts || []);
  const entryTypeCount = (schema.ledgerEntries?.types || []).length;
  return {
    key: schema.key,
    name: schema.name || "Not specified",
    accountCount,
    entryTypeCount
  };
};
var isJsonParseError = (error) => {
  return error.isParseError === true || error.message?.includes("Failed to parse");
};

export {
  validateSchemaStructure,
  countAccountsRecursively,
  extractSchemaMetadata,
  isJsonParseError
};
