import { ColorSpecificationSchema, FunctionSpecificationSchema, Config } from './chunk-RBQJJOOA.js';
import { type } from 'arktype';

var SchemaContentSchema = ColorSpecificationSchema.or(FunctionSpecificationSchema);
var TokenScriptSchemaResponseSchema = type({
  id: "string",
  type: "string",
  schema: "string",
  slug: "string",
  version: "string",
  content: SchemaContentSchema,
  "license_name?": "string | null"
});
async function fetchTokenScriptSchema(schemaUri, options = {}) {
  const { timeout = 1e4, headers = {}, signal } = options;
  const controller = signal ? new AbortController() : new AbortController();
  const timeoutId = setTimeout(() => controller.abort(), timeout);
  if (signal) {
    signal.addEventListener("abort", () => controller.abort());
  }
  try {
    const response = await fetch(schemaUri, {
      signal: controller.signal,
      headers: {
        Accept: "application/json",
        "User-Agent": "TokenScript-Interpreter/1.0",
        ...headers
      }
    });
    clearTimeout(timeoutId);
    if (!response.ok) {
      throw new Error(`HTTP error! status: ${response.status} - ${response.statusText}`);
    }
    const data = await response.json();
    const validatedData = TokenScriptSchemaResponseSchema(data);
    if (validatedData instanceof type.errors) {
      throw new Error(`Invalid schema structure: ${validatedData.summary}`);
    }
    return validatedData;
  } catch (error) {
    clearTimeout(timeoutId);
    if (error instanceof type.errors) {
      throw new Error(`Invalid schema structure: ${error.summary}`);
    }
    if (error instanceof Error) {
      if (error.name === "AbortError") {
        throw new Error(`Schema fetch timeout after ${timeout}ms`);
      }
      throw new Error(`Failed to fetch schema: ${error.message}`);
    }
    throw new Error("Unknown error occurred while fetching schema");
  }
}
async function fetchAndRegisterSchemas(schemaUris, config) {
  const configInstance = config || new Config();
  if (!schemaUris || schemaUris.length === 0) {
    return configInstance;
  }
  const schemas = await Promise.all(
    schemaUris.map(async (uri) => {
      const schemaResponse = await fetchTokenScriptSchema(uri);
      return {
        uri,
        schema: schemaResponse.content
      };
    })
  );
  return configInstance.registerSchemas(schemas);
}

export { fetchAndRegisterSchemas };
//# sourceMappingURL=chunk-6WXWYNOK.js.map
//# sourceMappingURL=chunk-6WXWYNOK.js.map