/**
 * 🧠 BRAINY EMBEDDED TYPE EMBEDDINGS
 *
 * AUTO-GENERATED - DO NOT EDIT
 * Generated: 2025-10-10T01:27:22.642Z
 * Noun Types: 31
 * Verb Types: 40
 *
 * This file contains pre-computed embeddings for all NounTypes and VerbTypes.
 * No runtime computation needed, instant availability!
 */

import { NounType, VerbType } from '../types/graphTypes.js'
import { Vector } from '../coreTypes.js'

// Type metadata
export const TYPE_METADATA = {
  nounTypes: 31,
  verbTypes: 40,
  totalTypes: 71,
  embeddingDimensions: 384,
  generatedAt: "2025-10-10T01:27:22.642Z",
  sizeBytes: {
    embeddings: 109056,
    base64: 145408
  }
}

// All noun types in order
const NOUN_TYPE_ORDER: NounType[] = ["person","organization","location","thing","concept","event","document","media","file","message","content","collection","dataset","product","service","user","task","project","process","state","role","topic","language","currency","measurement","hypothesis","experiment","contract","regulation","interface","resource"]

// All verb types in order
const VERB_TYPE_ORDER: VerbType[] = ["relatedTo","contains","partOf","locatedAt","references","precedes","succeeds","causes","dependsOn","requires","creates","transforms","becomes","modifies","consumes","owns","attributedTo","createdBy","belongsTo","memberOf","worksWith","friendOf","follows","likes","reportsTo","supervises","mentors","communicates","describes","defines","categorizes","measures","evaluates","uses","implements","extends","inherits","conflicts","synchronizes","competes"]

// Pre-computed embeddings (142.0KB base64)
const EMBEDDINGS_BASE64 = "[encoded data, 145408 characters]"

// Decode embeddings at startup (happens once, <10ms)
function decodeEmbeddings(): Uint8Array {
  if (typeof Buffer !== 'undefined') {
    // Node.js environment
    return Buffer.from(EMBEDDINGS_BASE64, 'base64')
  } else if (typeof atob !== 'undefined') {
    // Browser environment
    const binaryString = atob(EMBEDDINGS_BASE64)
    const bytes = new Uint8Array(binaryString.length)
    for (let i = 0; i < binaryString.length; i++) {
      bytes[i] = binaryString.charCodeAt(i)
    }
    return bytes
  }
  return new Uint8Array(0)
}

// Cached decoded embeddings
let decodedEmbeddings: Uint8Array | null = null

/**
 * Get noun type embeddings as a Map for fast lookup
 * This is called once and cached
 */
export function getNounTypeEmbeddings(): Map<NounType, Vector> {
  if (!decodedEmbeddings) {
    decodedEmbeddings = decodeEmbeddings()
  }

  const embeddings = new Map<NounType, Vector>()
  const view = new DataView(decodedEmbeddings.buffer)
  const embeddingSize = 384

  NOUN_TYPE_ORDER.forEach((type, index) => {
    const offset = index * embeddingSize * 4
    const embedding = new Float32Array(embeddingSize)

    for (let i = 0; i < embeddingSize; i++) {
      embedding[i] = view.getFloat32(offset + i * 4, true)
    }

    embeddings.set(type, Array.from(embedding))
  })

  return embeddings
}

/**
 * Get verb type embeddings as a Map for fast lookup
 * This is called once and cached
 */
export function getVerbTypeEmbeddings(): Map<VerbType, Vector> {
  if (!decodedEmbeddings) {
    decodedEmbeddings = decodeEmbeddings()
  }

  const embeddings = new Map<VerbType, Vector>()
  const view = new DataView(decodedEmbeddings.buffer)
  const embeddingSize = 384

  // Verb embeddings start after noun embeddings
  const verbStartOffset = 31 * embeddingSize * 4

  VERB_TYPE_ORDER.forEach((type, index) => {
    const offset = verbStartOffset + index * embeddingSize * 4
    const embedding = new Float32Array(embeddingSize)

    for (let i = 0; i < embeddingSize; i++) {
      embedding[i] = view.getFloat32(offset + i * 4, true)
    }

    embeddings.set(type, Array.from(embedding))
  })

  return embeddings
}

// Import logging
import { prodLog } from '../utils/logger.js'
prodLog.info(`🧠 Brainy Type Embeddings loaded: ${TYPE_METADATA.nounTypes} nouns, ${TYPE_METADATA.verbTypes} verbs, ${(TYPE_METADATA.sizeBytes.embeddings / 1024).toFixed(1)}KB`)
