"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 http_1 = require("@angular/http");
const material_1 = require("@angular/material");
const store_1 = require("@ngrx/store");
const moment = require("moment");
const Observable_1 = require("rxjs/Observable");
const project_1 = require("../redux/project");
const env_1 = require("../util/env");
const memorizingSubject_1 = require("../util/memorizingSubject");
const report_issue_1 = require("../util/report-issue");
/**
 * The code component displays the code editor for participants/control state/
 */
let HistoryComponent = class HistoryComponent {
    constructor(store, project, http, snackRef) {
        this.store = store;
        this.project = project;
        this.http = http;
        this.snackRef = snackRef;
        /**
         * When the user has an old save loaded out, we'll stash data about it
         * here so we can reload it again later.
         */
        this.swappedSave = new memorizingSubject_1.MemorizingSubject();
        /**
         * An enumeration of the available save files.
         */
        this.saves = this.http
            .get(env_1.apiUrl('saves'))
            .map(res => res.json())
            .map(list => list.reverse().map(entry => ({
            name: entry.name,
            savedAt: moment(entry.savedAt),
            ago: moment(entry.savedAt).fromNow(),
        })));
    }
    /**
     * Restores a backed up state.
     */
    loadBackupState() {
        const { backupState } = this.swappedSave.getValue();
        this.project.replaceState(backupState, true);
        this.swappedSave.next(null);
    }
    /**
     * openSaveFile restores state from a previously saved version.
     */
    openSaveFile(file) {
        Observable_1.Observable.combineLatest(this.http.get(env_1.apiUrl(`saves/${file}`)), this.store.map(project_1.getStoredData))
            .take(1)
            .switchMap(([next, previous]) => this.http
            .post(env_1.apiUrl('saves'), previous)
            .do(() => this.swappedSave.next({ backupState: previous, loadedSave: file }))
            .mapTo(next))
            .subscribe(state => this.project.replaceState(state.json().contents, true), err => this.snackRef
            .open('An unknown error occurred', 'Report', { duration: 5000 })
            .onAction()
            .subscribe(() => {
            report_issue_1.reportHttpError('Unknown error loading a save file', err);
        }));
    }
};
HistoryComponent = __decorate([
    core_1.Component({
        selector: 'editor-history',
        templateUrl: './history.component.html',
        styleUrls: ['./history.component.scss'],
        changeDetection: core_1.ChangeDetectionStrategy.OnPush,
    }),
    __metadata("design:paramtypes", [store_1.Store,
        project_1.ProjectService,
        http_1.Http,
        material_1.MatSnackBar])
], HistoryComponent);
exports.HistoryComponent = HistoryComponent;
