/*
 * Copyright (c) [2015] - [2017] Red Hat, Inc.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *   Red Hat, Inc. - initial API and implementation
 */
'use strict';
import {ShareWorkspaceController} from '../share-workspace.controller';

/**
 * This class is handling the controller for sharing a private workspace with developers.
 * @author Oleksii Kurinnyi
 */
export class AddDeveloperController {
  /**
   * Promises service.
   */
  private $q: ng.IQService;
  /**
   * Service for displaying dialogs.
   */
  private $mdDialog: ng.material.IDialogService;
  /**
   * true if user owns the workspace.
   */
  private canShare: boolean;
  /**
   * List of users to share the workspace.
   */
  private existingUsers: string[];
  /**
   * Parent controller.
   */
  private callbackController: ShareWorkspaceController;

  /**
   * Default constructor.
   * @ngInject for Dependency injection
   */
  constructor($q: ng.IQService, $mdDialog: ng.material.IDialogService) {
    this.$q = $q;
    this.$mdDialog = $mdDialog;
  }

  /**
   * Callback of the cancel button of the dialog.
   */
  abort() {
    this.$mdDialog.hide();
  }

  /**
   * Callback of the share button of the dialog.
   */
  
() {
    let users = [];
    this.existingUsers.forEach((userId: string) => {
      users.push({userId: userId, isTeamAdmin: false});
    });

    let permissionPromises = this.callbackController.shareWorkspace(users);

    this.$q.all(permissionPromises).then(() => {
      this.$mdDialog.hide();
    });
  }
}

shareWorkspace