"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
Object.defineProperty(exports, "__esModule", { value: true });
const core_1 = require("@angular/core");
const store_1 = require("@ngrx/store");
const Subject_1 = require("rxjs/Subject");
require("rxjs/add/operator/distinctUntilChanged");
require("rxjs/add/operator/map");
const memorizingSubject_1 = require("../util/memorizingSubject");
const devices_1 = require("./devices");
/**
 * The ControlStateSyncService handles synchronizing the device state and
 * video position between local and remote controls and the editor.
 */
let ControlStateSyncService = class ControlStateSyncService {
    constructor(store) {
        this.store = store;
        this.videoSizeSubj = new memorizingSubject_1.MemorizingSubject();
        this.fittedSizeSubj = new memorizingSubject_1.MemorizingSubject();
        this.refreshSubj = new Subject_1.Subject();
    }
    /**
     * Updates the store video size settings.
     */
    setVideoSize(options) {
        this.videoSizeSubj.next(options);
    }
    /**
     * Updates the fitted size of the video sent to controls.
     */
    updateFittedVideoSize(rect) {
        this.fittedSizeSubj.next(rect);
    }
    /**
     * Observable that emits when the video size/position changes.
     */
    getVideoSize() {
        return this.videoSizeSubj;
    }
    /**
     * Observable of the fitted, actual position/size of the video relative to the frame.
     */
    getFittedVideoSize() {
        return this.fittedSizeSubj;
    }
    /**
     * Returns an observable that fires when the user asks to refresh the frame.
     */
    getRefresh() {
        return this.refreshSubj;
    }
    /**
     * Asks the display iframe to refresh.
     */
    requestRefresh() {
        this.refreshSubj.next();
    }
    /**
     * Returns an observable of ISettings to share with the controls. Fires
     * immediately when first subscribed to.
     */
    getSettings() {
        return this.store
            .select('frame')
            .map(frame => devices_1.devices[frame.chosenDevice].isMobile)
            .distinctUntilChanged()
            .map(isMobile => ({
            language: navigator.language,
            placesVideo: !isMobile,
            platform: (isMobile ? 'mobile' : 'desktop'),
        }));
    }
};
ControlStateSyncService = __decorate([
    core_1.Injectable(),
    __metadata("design:paramtypes", [store_1.Store])
], ControlStateSyncService);
exports.ControlStateSyncService = ControlStateSyncService;
