import { Configuration } from "./Configuration" ; import { Store } from "./Store" ; import { Configurator , Joiner } from "../calculation" ; import type { JConfiguration , JOverlap , JStretch , JStructure , Memento } from "bp/content/json" ; import type { Stretch } from ".." ; interface JRepository { configurations : JConfiguration [ ] ; index : number ; } /**
 *  Repository 是針對  Stretch 的特定配置算出的若干套  Configuration 的組合。
 *
 *  Repository 物件存在的動機是為了使得當  Stretch 的配置暫時發生改變、
 * 或是  Stretch 由於拖曳而暫時解除活躍時，可以記住原本的  Pattern 組合。
  @ shrewd export class Repository extends Store < Configuration , Configuration > implements ISerializable < JRepository > { public get $tag ( ) : string { return "r" + this . $stretch . $signature ; } public readonly $stretch : Stretch ; public readonly $signature : string ; public readonly $structure : JStructure ; protected $generator : Generator < Configuration > ; private readonly joinerCache : Map < string , Joiner > = new Map ( ) ; protected $builder ( prototype ) { return prototype ; } constructor ( stretch , signature , option ? ) { super ( stretch . $sheet ) ; this . $stretch = stretch ; this . $signature = signature ; this . $structure = JSON . parse ( signature ) ; let json = stretch . $design . $options . get ( this ) ; if ( json ) { this . $restore ( json . configurations . map ( c => new Configuration ( this , c ) ) , json . index ) ; } else { this . $generator = new Configurator ( this , option ) . $generate ( ( ) => this . joinerCache . clear ( ) ) ; } } private _everActive : boolean = false ; /** 一旦安定下來了之後就記錄自己的建構 */ @ shrewd private _onSettle ( ) { if ( ! this . _everActive && this . _isActive && ! this . $design . $dragging ) { this . _everActive = true ; this . $design . $history ?. $construct ( this . $toMemento ( ) ) ; } } protected get $shouldDispose ( ) : boolean { let active = this . _isActive , dragging = this . $design . $dragging ; return super . $shouldDispose || this . $stretch . $disposed || ! active && ! dragging ; } protected $onDispose ( ) { if ( this . _everActive ) this . $design ?. $history ?. $destruct ( this . $toMemento ( ) ) ; super . $onDispose ( ) ; } @ shrewd public get _isActive ( ) : boolean { return this . $stretch . _isActive && this . $stretch . $repository == this ; } protected $onMove ( ) { this . $stretch . $selected = ! this . entry ! . entry ! . $selected ; } /** 根據  JOverlap 組合來產生（或沿用）一個  Joiner public getJoiner ( overlaps ) { let key = JSON . stringify ( overlaps ) ; let j = this . joinerCache . get ( key ) ; if ( ! j ) this . joinerCache . set ( key , j = new Joiner ( overlaps , this ) ) ; return j ; } public toJSON ( ) { return { configurations : this . $memento . map ( c => c . toJSON ( true ) ) , index : this . index , } ; } public $toMemento ( ) { return [ this . $tag , this . toJSON ( ) ] ; } }