/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/
import CryptoFactory from './crypto/plugin/CryptoFactory';
import SubtleCryptoBrowser from './crypto/plugin/SubtleCryptoBrowser';
import KeyStoreInMemory from './crypto/keyStore/KeyStoreInMemory';
import { IPayloadProtection } from './crypto/protocols/IPayloadProtection';
import JoseProtocol from './crypto/protocols/jose/JoseProtocol';
import { SubtleCryptoNode, IKeyStore } from '.';

/**
 * Class used to model crypto options
 */
export default class CryptoOptions {
  private subtle = () => {
    let subtleCrypto: any;
    try {
      subtleCrypto = SubtleCryptoBrowser.getSubtleCrypto();
    } catch {
      subtleCrypto = SubtleCryptoNode.getSubtleCrypto();  
    }
    return subtleCrypto;
  };

  /**
   * Instanciate a new instance of @class CrytoOptions
   * @param cryptoFactory Optional parameter specifying the crypto factory
   */
constructor(cryptoFactory?: CryptoFactory) {
  if (cryptoFactory) {
    this.cryptoFactory = cryptoFactory;
  } else {
    this.cryptoFactory = new CryptoFactory(new KeyStoreInMemory(), this.subtle());
  }
}

  /**
   * Get or set the crypto api to be used. Initialize the default crypto plugin.
   */
  public cryptoFactory: CryptoFactory;

  /**
   * Get or set the payload protection protocol.
   */
  public payloadProtection: IPayloadProtection = new JoseProtocol();

  /**
   * Get or set the signing algorithm.
   */
  public signingAlgorithm: string = 'ES256K';

  /**
   * Get or set the encryption algorithm.
   */
  public encryptionAlgorithm: string = 'RSA-OAEP';

   /**
   * Key reference to private key to be used to sign commits and create HubSession
   */
  public signingKeyReference: string | undefined;

   /**
   * Key reference to private key to be used to decrypt commits and create HubSession
   */
  public encryptionKeyReference: string | undefined;
}
