/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/
import { KeyUse } from './KeyUseFactory';
import { KeyType } from './KeyTypeFactory';
import IKeyContainer, { CryptographicKey } from './IKeyContainer';

/**
 * Represents a Key container in JWK format.
 * A key container will hold different versions of JWK keys.
 * Each key in the key container is the same type and usage
 */
export default class KeyContainer implements IKeyContainer {
  private keysInContainer: CryptographicKey[];

   /**
    * Create instance of @class KeyContainer
    */
  constructor (key: CryptographicKey) {
    this.keysInContainer = [key];
  }

   /**
    * Return all keys in the container
    */
  public get keys (): CryptographicKey[] {
    return this.keysInContainer;
  }

  /**
   * Key type
   */
  public get kty (): KeyType {
    return this.keysInContainer[0].kty;
  }

  /**
   * Intended use
   */
  public get use (): KeyUse | undefined {
    return this.keysInContainer[0].use;
  }

  /**
   * Algorithm intended for use with this key
   */
  public get alg (): string | undefined {
    return this.keysInContainer[0].alg;
  }

  /**
   * Algorithm intended for use with this key
   */
  public add (key: CryptographicKey): void {
    // Check for valid key to add
    this.keysInContainer.push(key);
  }

   /**
    * Get the default key from the key container
    */
  public getKey<T= CryptographicKey> (): T {
     // return last keys as reference
    return (<any>this.keysInContainer)[this.keysInContainer.length - 1];
  }

}
