"use strict";

var _interopRequireDefault = require("@babel/runtime-corejs3/helpers/interopRequireDefault");

var _defineProperty2 = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/object/define-property"));

var _defineProperties = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/object/define-properties"));

var _getOwnPropertyDescriptors = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/object/get-own-property-descriptors"));

var _forEach = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/for-each"));

var _getOwnPropertyDescriptor = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/object/get-own-property-descriptor"));

var _filter = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/filter"));

var _getOwnPropertySymbols = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/object/get-own-property-symbols"));

var _keys = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/object/keys"));

var _defineProperty3 = _interopRequireDefault(require("@babel/runtime-corejs3/helpers/defineProperty"));

var _jwtDecode = _interopRequireDefault(require("jwt-decode"));

function ownKeys(object, enumerableOnly) { var keys = (0, _keys.default)(object); if (_getOwnPropertySymbols.default) { var symbols = (0, _getOwnPropertySymbols.default)(object); if (enumerableOnly) symbols = (0, _filter.default)(symbols).call(symbols, function (sym) { return (0, _getOwnPropertyDescriptor.default)(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { var _context; (0, _forEach.default)(_context = ownKeys(source, true)).call(_context, function (key) { (0, _defineProperty3.default)(target, key, source[key]); }); } else if (_getOwnPropertyDescriptors.default) { (0, _defineProperties.default)(target, (0, _getOwnPropertyDescriptors.default)(source)); } else { var _context2; (0, _forEach.default)(_context2 = ownKeys(source)).call(_context2, function (key) { (0, _defineProperty2.default)(target, key, (0, _getOwnPropertyDescriptor.default)(source, key)); }); } } return target; }

console.log('COMMANDS IS RUNNING!!!!');
const _ = Cypress._;
const api = 'http://idsrv.mmsci-merchanthub-dev.dev.meniga.net/identity/connect/token';
const defaultParams = {
  client_id: 'automatic_testing_act_as',
  client_secret: 'hal-and-dave',
  scope: 'openid email profile mms_hub_api mms_api roles',
  grant_type: 'password'
};
let user = null;
Cypress.Commands.add('login', (username = 'root@meniga.com', password = 'rootAdmin321', params = defaultParams) => cy.request({
  url: api,
  body: _objectSpread({}, params, {
    username: username || params.username,
    password: password || params.password
  }),
  form: true,
  method: 'POST'
}).then(res => {
  const tokenData = (0, _jwtDecode.default)(res.body.access_token);
  cy.log(`Login successfull for ${username}`);
  user = {
    expires_at: tokenData.exp,
    scope: tokenData.scope,
    token_type: res.body.token_type,
    access_token: res.body.access_token,
    profile: tokenData
  };
}));
Cypress.Commands.overwrite('visit', (originalFn, url, options) => {
  const originalOnBeforeLoad = !!options ? options.onBeforeLoad : null;

  const newOptions = _.assign(options, {
    onBeforeLoad: win => {
      win.M_USER = user;
      win.localStorage.accessToken = Cypress.env('currentUser') ? Cypress.env('currentUser').access_token : null;
      if (!!originalOnBeforeLoad) originalOnBeforeLoad(win);
    }
  });

  return originalFn(url, newOptions);
});
Cypress.Commands.add('getId', {
  prevSubject: ['optional', 'window', 'document', 'element']
}, (subject, id) => {
  if (subject) return cy.get(`${subject.selector} > [data-test-id=${id}]`);
  return cy.get(`[data-test-id=${id}]`);
});