"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 BehaviorSubject_1 = require("rxjs/BehaviorSubject");
const http_error_service_1 = require("../http-error.service");
const env_1 = require("../util/env");
/**
 * The rename bundle dialog is opened when the user tries to upload a bundle,
 * but we get a 403 saying they don't have access to it (someone else owns it).
 * We prompt them to rename it, the dialog will return whether they have
 * successfully done so (true) or cancelled it (false).
 */
let RenameBundleDialogComponent = class RenameBundleDialogComponent {
    constructor(http, dialogRef, snackRef, httpErr) {
        this.http = http;
        this.dialogRef = dialogRef;
        this.snackRef = snackRef;
        this.httpErr = httpErr;
        /**
         * Whether the dialog is submitting.
         */
        this.isLoading = new BehaviorSubject_1.BehaviorSubject(true);
        /**
         * Currently chosen bundle name.
         */
        this.name = new BehaviorSubject_1.BehaviorSubject('');
    }
    submit() {
        const name = this.name.getValue().trim();
        if (!/^[a-z0-9-]+$/.test(name)) {
            this.snackRef.open('Your name may only contain alphanumeric characters and dashes.', undefined, { duration: 5000 });
            return;
        }
        this.isLoading.next(true);
        this.http
            .post(env_1.apiUrl('modify-package'), { name })
            .toPromise()
            .then(() => {
            this.dialogRef.close(true);
        })
            .catch(this.httpErr.catch('BundleNameTakenErrorBundleNameTakenError', () => {
            this.snackRef.open('That bundle name is taken! Please choose a new one.', undefined, {
                duration: 5000,
            });
        }))
            .catch(this.httpErr.toast);
    }
    ngOnInit() {
        this.http
            .get(env_1.apiUrl('metadata'))
            .toPromise()
            .then(res => {
            const config = res.json();
            this.name.next(config.name);
            this.isLoading.next(false);
        })
            .catch(this.httpErr.toast);
    }
    alertTaken() {
        this.snackRef.open('That bundle name is taken! Please choose a new one.', undefined, {
            duration: 5000,
        });
    }
};
RenameBundleDialogComponent = __decorate([
    core_1.Component({
        selector: 'rename-bundle-dialog',
        templateUrl: './rename-bundle-dialog.component.html',
        styleUrls: ['../dialog.scss'],
        changeDetection: core_1.ChangeDetectionStrategy.OnPush,
    }),
    __metadata("design:paramtypes", [http_1.Http,
        material_1.MatDialogRef,
        material_1.MatSnackBar,
        http_error_service_1.HttpErrorService])
], RenameBundleDialogComponent);
exports.RenameBundleDialogComponent = RenameBundleDialogComponent;
