import { File, type IFile } from '../models/store/File.js'
import type { DataDomain, DataDomainSchema } from './DataDomain.js'
import { DomainFileKind } from '../models/kinds.js'

export interface IDomainFile extends IFile {
  kind: typeof DomainFileKind
}

/**
 * Used by the store. A file definition for the DataDomain
 */
export class DomainFile extends File {
  override kind = DomainFileKind

  static override fromName(name: string): DomainFile {
    return super.fromName(name, DomainFileKind) as DomainFile
  }

  /**
   * Creates the file definition for a DomainNamespace contents.
   *
   * @param input The data namespace instance or schema.
   */
  static fromDataDomain(input: DataDomain | DataDomainSchema): DomainFile {
    let final: DataDomainSchema
    if (typeof (input as DataDomain).toJSON === 'function') {
      final = (input as DataDomain).toJSON()
    } else {
      final = input as DataDomainSchema
    }
    return new DomainFile({ key: final.key, info: final.info })
  }

  constructor(state: Partial<IDomainFile> = {}) {
    super({ ...state, kind: DomainFileKind })
  }

  static isDomainFile(input: unknown): boolean {
    const typed = input as IDomainFile
    if (!input || typed.kind !== DomainFileKind) {
      return false
    }
    return true
  }

  override toJSON(): IDomainFile {
    const result: IDomainFile = {
      ...super.toJSON(),
      kind: DomainFileKind,
    }
    return result
  }
}
