"use strict";

var _jsdom = require("jsdom");

var _testUtils = _interopRequireDefault(require("react-dom/test-utils"));

var _react = _interopRequireDefault(require("react"));

var _propTypes = _interopRequireDefault(require("prop-types"));

var _reactDom = _interopRequireDefault(require("react-dom"));

var _xhr = _interopRequireDefault(require("xhr2"));

var _nock = _interopRequireDefault(require("nock"));

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

//$Id$//
let mockDomain = 'htt' + 'p://zoho.com';
const {
  document
} = new _jsdom.JSDOM('').window;
global.document = document;
global.window = document.defaultView;
global.navigator = global.window.navigator;
global.localStorage = global.sessionStorage = {
  getItem(key) {
    return this[key];
  },

  setItem(key, value) {
    if (value.length > 100) {
      throw new Error('Data size is too exceeded');
    }

    this[key] = value;
  },

  removeItem(key) {
    delete this[key];
  },

  clear() {
    let keys = ['getItem', 'setItem', 'removeItem', 'clear'];

    for (let key in this) {
      if (keys.indexOf(key) === -1) {
        delete this[key];
      }
    }
  }

};
global.ZE_Init = {};

global.String.prototype.contains = function (text) {
  return this.indexOf(text) != -1;
};

global.TestUtils = _testUtils.default;
let xmlReq = _xhr.default;

window.XMLHttpRequest = function () {
  let xmlReqCopy = new xmlReq();
  let originalOpen = xmlReqCopy.open;

  xmlReqCopy.open = function () {
    if (arguments[1].indexOf('http') != 0) {
      arguments[1] = mockDomain + arguments[1];
    }

    return originalOpen.apply(this, arguments);
  };

  return xmlReqCopy;
};

_testUtils.default.scryRenderedComponentsWithTestid = function (dom, name) {
  let componentList = _testUtils.default.findAllInRenderedTree(dom, (i, j) => {
    if (_testUtils.default.isDOMComponent(i)) {
      let val = i.getAttribute('data-id');

      if (typeof val !== 'undefined' && val == name) {
        return true;
      }

      return false;
    }
  });

  return componentList;
};

_testUtils.default.findRenderedComponentsWithTestid = function (dom, name) {
  let list = _testUtils.default.scryRenderedComponentsWithTestid(dom, name);

  if (list.length !== 1) {
    throw new Error(`Did not find exactly one match (found: ${list.length}) ` + `for data-id:${name}`);
  }

  return list[0];
};

global.render = function (Component, props) {
  const renderedDOM = _testUtils.default.renderIntoDocument( /*#__PURE__*/_react.default.createElement(Component, props));

  return {
    props,
    renderedDOM
  };
};

global.setup = function (Component, props, state) {
  let router = {
    router: {
      push() {},

      createHref(ob) {
        return ob.pathname;
      },

      isActive() {
        return true;
      },

      replace() {},

      go() {},

      goBack() {},

      goForward() {},

      setRouteLeaveHook() {},

      getState() {}

    },
    store: {
      getState() {
        return state;
      }

    }
  }; // var store = {
  // 	store:{
  // 		getState:function(){return state;}
  // 	}
  // }

  var Component = higherComponent(Component, router);

  const renderedDOM = _testUtils.default.renderIntoDocument( /*#__PURE__*/_react.default.createElement(Component, props), router);

  return {
    props,
    renderedDOM
  };
};

function higherComponent(ActualComponent, context) {
  if (context) {
    class HigherComponent extends _react.default.Component {
      constructor() {
        super();
      }

      getChildContext() {
        return context;
      }

      render() {
        return /*#__PURE__*/_react.default.createElement(ActualComponent, this.props);
      }

    }

    HigherComponent.childContextTypes = {
      router: _propTypes.default.any,
      store: _propTypes.default.any
    };
    return HigherComponent;
  }

  return ActualComponent;
}

global.window.matchMedia = window.matchMedia || function () {
  return {
    matches: false,

    addListener() {},

    removeListener() {}

  };
}; // global.renderHTML = function(comp) {
//   return ReactDOM.findDOMNode(comp);
// };


global.TestUtils = _testUtils.default;
global.XMLHttpRequest = window.XMLHttpRequest;

global.getI18NValue = function (inp) {
  return inp;
};