import _defineProperty from '@babel/runtime/helpers/defineProperty';
import TKey from '@tkey/core';
import { ServiceProviderBase } from '@tkey/service-provider-base';
import { TorusServiceProvider } from '@tkey/service-provider-torus';
import { SHARE_SERIALIZATION_MODULE_NAME, ShareSerializationModule } from '@tkey/share-serialization';
import { SHARE_TRANSFER_MODULE_NAME, ShareTransferModule } from '@tkey/share-transfer';
import { TorusStorageLayer, MockStorageLayer } from '@tkey/storage-layer-torus';

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
class ThresholdKey extends TKey {
  constructor(args) {
    const {
      modules = {},
      serviceProvider,
      storageLayer,
      customAuthArgs,
      serverTimeOffset
    } = args || {};
    const defaultModules = {
      [SHARE_TRANSFER_MODULE_NAME]: new ShareTransferModule(),
      [SHARE_SERIALIZATION_MODULE_NAME]: new ShareSerializationModule()
    };
    let finalServiceProvider;
    let finalStorageLayer;
    if (!serviceProvider) {
      finalServiceProvider = new TorusServiceProvider({
        customAuthArgs
      });
    } else {
      finalServiceProvider = serviceProvider;
    }
    if (!storageLayer) {
      finalStorageLayer = new TorusStorageLayer({
        hostUrl: "https://metadata.tor.us",
        serverTimeOffset
      });
    } else {
      finalStorageLayer = storageLayer;
    }
    super(_objectSpread(_objectSpread({}, args || {}), {}, {
      modules: _objectSpread(_objectSpread({}, defaultModules), modules),
      serviceProvider: finalServiceProvider,
      storageLayer: finalStorageLayer
    }));
  }
  static async fromJSON(value, args) {
    const {
      storageLayer: tempOldStorageLayer,
      serviceProvider: tempOldServiceProvider
    } = value;
    const {
      storageLayer,
      serviceProvider,
      modules = {},
      customAuthArgs,
      serverTimeOffset = 0
    } = args || {};
    const defaultModules = {
      [SHARE_TRANSFER_MODULE_NAME]: new ShareTransferModule(),
      [SHARE_SERIALIZATION_MODULE_NAME]: new ShareSerializationModule()
    };
    const finalServiceProvider = serviceProvider || TorusServiceProvider.fromJSON(tempOldServiceProvider) || ServiceProviderBase.fromJSON(tempOldServiceProvider) || new TorusServiceProvider({
      customAuthArgs
    });
    tempOldStorageLayer.serviceProvider = finalServiceProvider;
    const finalStorageLayer = storageLayer || MockStorageLayer.fromJSON(tempOldStorageLayer) || TorusStorageLayer.fromJSON(tempOldStorageLayer) || new TorusStorageLayer({
      hostUrl: "https://metadata.tor.us",
      serverTimeOffset
    });
    return super.fromJSON(value, _objectSpread(_objectSpread({}, args || {}), {}, {
      modules: _objectSpread(_objectSpread({}, defaultModules), modules),
      serviceProvider: finalServiceProvider,
      storageLayer: finalStorageLayer
    }));
  }
}

export { ThresholdKey as default };
//# sourceMappingURL=default.esm.js.map
