import _extends from "@babel/runtime/helpers/extends";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
var _excluded = ["channel", "selected", "accounts", "email", "onLogin"];

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; }

import React, { useContext, useMemo } from "react";
import PlatformAccounts from "../../../ui/PlatformAccounts";
import { TranslationsContext } from "./context";
import { useTrackEvent } from "../events";

function AccountSelector(_ref) {
  var channel = _ref.channel,
      selected = _ref.selected,
      accounts = _ref.accounts,
      email = _ref.email,
      onLogin = _ref.onLogin,
      restProps = _objectWithoutProperties(_ref, _excluded);

  var translations = useContext(TranslationsContext);
  var trackEvent = useTrackEvent();
  var processedAccounts = useMemo(function () {
    return accounts.map(function (account) {
      // Selected account
      if (account.id === (selected === null || selected === void 0 ? void 0 : selected.value)) {
        var _account$subAccounts;

        return _objectSpread(_objectSpread({}, account), {}, {
          switchActive: true,
          subAccounts: account === null || account === void 0 ? void 0 : (_account$subAccounts = account.subAccounts) === null || _account$subAccounts === void 0 ? void 0 : _account$subAccounts.map(function (subAccount) {
            // Selected subaccount
            if (subAccount.id === (selected === null || selected === void 0 ? void 0 : selected.childValue)) {
              return _objectSpread(_objectSpread({}, subAccount), {}, {
                switchActive: true
              });
            }

            return _objectSpread(_objectSpread({}, subAccount), {}, {
              switchActive: false
            });
          })
        });
      }

      return _objectSpread(_objectSpread({}, account), {}, {
        switchActive: false
      });
    });
  }, [accounts, selected === null || selected === void 0 ? void 0 : selected.value, selected === null || selected === void 0 ? void 0 : selected.childValue]);

  var handleLogin = function handleLogin() {
    trackEvent({
      key: "step_connect_click_connect",
      platform: channel
    });
    onLogin();
  };

  var texts = {
    addAccount: translations.connect.add_account,
    connect: translations.connect.connect_account,
    changeAccount: translations.connect.change_account,
    link: translations.connect.link_account,
    warningTooltip: translations.connect.warning_tooltip,
    addSubAccount: translations.connect.add_account,
    noAccounts: translations.connect.no_accounts
  };
  return /*#__PURE__*/React.createElement(PlatformAccounts, _extends({
    channel: channel,
    accounts: processedAccounts,
    connectedEmail: email,
    texts: texts,
    initialOpen: true,
    onLogin: handleLogin
  }, restProps));
}

export default AccountSelector;