import { of } from 'rxjs';
import { map } from 'rxjs/operators';
import { ViewModel } from '../../../config/view/view.model';
import { StringifyingMap } from '../../../core/stringifying-map';
export class RemoteViewStore {
    constructor(remoteViewProtocol) {
        this.remoteViewProtocol = remoteViewProtocol;
        this.viewConfigCache = new StringifyingMap(k => JSON.stringify(k));
    }
    getMainViewConfig() {
        return this.remoteViewProtocol.getReleasedMainView().pipe(map(view => {
            const result = new ViewModel(view.content);
            this.viewConfigCache.set(view.viewIndex, result);
            return result;
        }));
    }
    getViewConfig(viewIndex) {
        if (this.viewConfigCache.has(viewIndex)) {
            return of(this.viewConfigCache.get(viewIndex));
        }
        return this.remoteViewProtocol.getReleasedView(viewIndex).pipe(map(config => {
            const result = new ViewModel(config.content);
            this.viewConfigCache.set(viewIndex, result);
            return result;
        }));
    }
    getViewConfigByConfigureViewId(configureViewId) {
        return this.remoteViewProtocol.getViewConfigByConfigureViewId(configureViewId).pipe(map(config => {
            const result = new ViewModel(config.content);
            return result;
        }));
    }
    getViewConfigByConfigureViewCode(configureViewCode) {
        return this.remoteViewProtocol.getViewConfigByConfigureViewCode(configureViewCode).pipe(map(config => {
            const result = new ViewModel(config.content);
            return result;
        }));
    }
}
