(function() {
  function tKey(key) {
    return typeof t === 'function' ? t(key) : key;
  }

  var defaults = {
    currentStep: 1,
    wizardProtocol: 'rest',
    wizardMode: 'create',
    wizardEditId: null,
    wizardWorkspaceId: null,
    wizardCurrentOperation: null,
    wizardCurrentVersion: null,
    wizardProtoUpload: null,
    wizardDescriptorSetUpload: null,
    wizardSoapWsdlUpload: null,
    wizardSoapXsdUpload: null,
    wizardTestResponsePreview: null,
    grpcDescriptorServices: [],
    soapServiceCatalog: [],
    wizardProtocolCapabilities: null,
    wizardEditionCapabilities: null,
    wizardSecrets: [],
    wizardAuthProfiles: [],
    selectedUpstreamId: null,
    editingUpstreamId: null,
    protoParsed: null,
    selectedRpcMethod: null,
  };

  Object.keys(defaults).forEach(function(key) {
    if (typeof window[key] !== 'undefined') {
      return;
    }
    var value = defaults[key];
    if (Array.isArray(value)) {
      window[key] = value.slice();
      return;
    }
    if (value && typeof value === 'object') {
      window[key] = Object.assign({}, value);
      return;
    }
    window[key] = value;
  });

  function defaultProtocolCapabilities() {
    return {
      rest: {
        supports_execution_modes: ['unary'],
        supports_transport_behaviors: ['request_response'],
      },
      graphql: {
        supports_execution_modes: ['unary'],
        supports_transport_behaviors: ['request_response'],
      },
      grpc: {
        supports_execution_modes: ['unary'],
        supports_transport_behaviors: ['request_response'],
      },
      websocket: {
        supports_execution_modes: ['unary'],
        supports_transport_behaviors: ['request_response'],
      },
      soap: {
        supports_execution_modes: ['unary'],
        supports_transport_behaviors: ['request_response'],
      },
    };
  }

  function defaultEditionCapabilities() {
    return {
      edition: 'community',
      supported_protocols: ['rest', 'graphql', 'grpc'],
      supported_security_levels: ['standard'],
      machine_access_modes: ['static_agent_key'],
      limits: {
        max_workspaces: 1,
        max_users_per_workspace: 1,
        max_agents_per_workspace: 1,
      },
    };
  }

  function currentProtocolCapabilities() {
    var capabilities = window.wizardProtocolCapabilities || defaultProtocolCapabilities();
    return capabilities[window.wizardProtocol] || capabilities.rest;
  }

  function currentEditionCapabilities() {
    return window.wizardEditionCapabilities || defaultEditionCapabilities();
  }

  async function loadProtocolCapabilities() {
    if (!window.wizardWorkspaceId || !window.CrankApi || typeof window.CrankApi.getProtocolCapabilities !== 'function') {
      window.wizardProtocolCapabilities = defaultProtocolCapabilities();
      return window.wizardProtocolCapabilities;
    }

    try {
      var response = await window.CrankApi.getProtocolCapabilities(window.wizardWorkspaceId);
      var mapped = {};
      (response.items || []).forEach(function(item) {
        mapped[item.protocol] = item;
      });
      window.wizardProtocolCapabilities = mapped;
    } catch (_error) {
      window.wizardProtocolCapabilities = defaultProtocolCapabilities();
    }

    return window.wizardProtocolCapabilities;
  }

  async function loadEditionCapabilities() {
    if (!window.CrankApi || typeof window.CrankApi.getCapabilities !== 'function') {
      window.wizardEditionCapabilities = defaultEditionCapabilities();
      return window.wizardEditionCapabilities;
    }

    try {
      window.wizardEditionCapabilities = await window.CrankApi.getCapabilities();
    } catch (_error) {
      window.wizardEditionCapabilities = defaultEditionCapabilities();
    }

    return window.wizardEditionCapabilities;
  }

  function step3Labels() {
    return {
      rest: tKey('wizard.step3.rest.label'),
      graphql: tKey('wizard.step3.graphql.label'),
      grpc: tKey('wizard.step3.grpc.label'),
      websocket: tKey('wizard.step3.websocket.label'),
      soap: tKey('wizard.step3.soap.label'),
    };
  }

  window.CrankWizardState = {
    state: window,
    defaultProtocolCapabilities: defaultProtocolCapabilities,
    defaultEditionCapabilities: defaultEditionCapabilities,
    currentProtocolCapabilities: currentProtocolCapabilities,
    currentEditionCapabilities: currentEditionCapabilities,
    loadProtocolCapabilities: loadProtocolCapabilities,
    loadEditionCapabilities: loadEditionCapabilities,
    step3Labels: step3Labels,
  };
}());
