license import { DetectableImage , DetectedImage } from '../../../defs/detected-image.js' ; import { typeIsThing } from '../schema/core-schema-org.js' ; import { ARArtifact , ARImageTarget } from '../schema/extension-ar-artifacts.js' ; import { PerceptionResult } from './artifact-store.js' ; export class LocalImageStore { private readonly images = new Map < string , PerceptionResult > ( ) ; addImage ( artifact , imageTarget ) { if ( ! imageTarget . name ) { return false ; } this . images . set ( imageTarget . name , { target : imageTarget , artifact } ) ; return true ; } getDetectableImages ( ) { const allDetectableImages = [ ] ; for ( const { target } of this . images . values ( ) ) { if ( ! typeIsThing ( target ) || target [ '@type' ] !== 'ARImageTarget' || ! target . name ) { continue ; } const detectableImage = { 'id' : target . name , 'media' : [ ] , } ; if ( typeIsThing ( target . image ) ) { if ( target . image [ '@type' ] === 'ImageObject' ) { detectableImage . media . push ( target . image ) ; } } else if ( target . image ) { detectableImage . media . push ( { '@type' : 'ImageObject' , 'encodingUrl' : target . image } ) ; } if ( target . hasOwnProperty ( 'encoding' ) ) { detectableImage . media = detectableImage . media . concat ( target . encoding ) ; } if ( target . hasOwnProperty ( 'associatedMedia' ) ) { detectableImage . media = detectableImage . media . concat ( target . associatedMedia ) ; } allDetectableImages . push ( detectableImage ) ; } return allDetectableImages ; } findRelevantArtifacts ( detectedImages ) { const ret = [ ] ; for ( const detectedImage of detectedImages ) { const nearbyResult = this . images . get ( detectedImage . id ) ; if ( nearbyResult ) { ret . push ( nearbyResult ) ; } } return ret ; } }