import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
import _createClass from "@babel/runtime/helpers/esm/createClass";
import _possibleConstructorReturn from "@babel/runtime/helpers/esm/possibleConstructorReturn";
import _getPrototypeOf from "@babel/runtime/helpers/esm/getPrototypeOf";
import _inherits from "@babel/runtime/helpers/esm/inherits";
function _callSuper(t, o, e) { return o = _getPrototypeOf(o), _possibleConstructorReturn(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], _getPrototypeOf(t).constructor) : o.apply(t, e)); }
function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); }
import { Component } from 'react';
import ReactHtmlParser from 'react-html-parser';
var captcha_value = '';
var captcha_number = '';
var backgroundColor_value = '';
var fontColor_value = '';
var charMap_value = '';
var LoadCanvasTemplate_HTML = "<div><canvas id=\"canv\"></canvas><div><a id=\"reload_href\"  style=\"cursor: pointer; color: blue\">Reload Captcha</a></div></div>";
var LoadCanvasTemplateNoReload_HTML = "<div><canvas id=\"canv\"></canvas><div><a id=\"reload_href\"  style=\"cursor: pointer; color: blue\"></a></div></div>";
;
var _loadCaptchaEnginge = function loadCaptchaEnginge(numberOfCharacters) {
  var backgroundColor = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'white';
  var fontColor = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 'black';
  var charMap = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : '';
  backgroundColor_value = backgroundColor;
  fontColor_value = fontColor;
  charMap_value = charMap;
  captcha_number = numberOfCharacters;
  var retVal = "";
  var charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
  if (charMap === "upper") {
    charset = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
  } else if (charMap === "lower") {
    charset = "abcdefghijklmnopqrstuvwxyz0123456789";
  } else if (charMap === "numbers") {
    charset = "0123456789";
  } else if (charMap === "special_char") {
    charset = "~`!@#$%^&*()_+-=[]{}\|:'<>,.?/";
  }
  var length = parseInt(numberOfCharacters);
  for (var i = 0, n = charset.length; i < length; ++i) {
    retVal += charset.charAt(Math.floor(Math.random() * n));
  }
  var captcha = retVal;
  captcha_value = captcha;
  var length_height_canvas = Math.round(parseInt(length) / 3);
  var canvas = document.getElementById('canv'),
    ctx = canvas.getContext('2d'),
    img = document.getElementById('image');
  var text = captcha;
  var x = 12.5;
  var y = 15;
  var lineheight = 30;
  var canvas_height = (parseInt(length) - parseInt(length_height_canvas)) * 20;
  var lines = text.split('\n');
  var lineLengthOrder = lines.slice(0).sort(function (a, b) {
    return b.length - a.length;
  });
  ctx.canvas.width = parseInt(length) * 25;
  ctx.canvas.height = lines.length * lineheight;
  ctx.fillStyle = backgroundColor;
  ctx.fillRect(0, 0, canvas.width, canvas.height);
  ctx.textBaseline = "middle";
  ctx.font = "italic 20px Arial";
  ctx.fillStyle = fontColor;
  var num = 0;
  for (var _i = 0; _i < parseInt(length); _i++) {
    num = parseInt(num) + 1;
    var heigt_num = 20 * num;
    ctx.fillText(retVal[_i], heigt_num, Math.round(Math.random() * (15 - 12) + 12));
  }
  document.getElementById("reload_href").onclick = function () {
    _loadCaptchaEnginge(captcha_number, backgroundColor, fontColor, charMap);
  };
};
export { _loadCaptchaEnginge as loadCaptchaEnginge };
export var validateCaptcha = function validateCaptcha(userValue) {
  var reload = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true;
  if (userValue != captcha_value) {
    if (reload == true) {
      _loadCaptchaEnginge(captcha_number, backgroundColor_value, fontColor_value, charMap_value);
    }
    return false;
  } else {
    return true;
  }
};
export var LoadCanvasTemplate = /*#__PURE__*/function (_Component) {
  function LoadCanvasTemplate() {
    _classCallCheck(this, LoadCanvasTemplate);
    return _callSuper(this, LoadCanvasTemplate, arguments);
  }
  _inherits(LoadCanvasTemplate, _Component);
  return _createClass(LoadCanvasTemplate, [{
    key: "render",
    value: function render() {
      var reload_text = "";
      var reload_color = "";
      LoadCanvasTemplate_HTML = "<div><canvas id=\"canv\" style=\"background-color: blue;\"></canvas><div><a id=\"reload_href\"  style=\"cursor: pointer; color: blue\">Reload Captcha</a></div></div>";
      if (this.props.reloadText) {
        reload_text = this.props.reloadText;
      }
      if (this.props.reloadColor) {
        reload_color = this.props.reloadColor;
      }
      if (reload_text == "") {
        reload_text = "Reload Captcha";
      }
      if (reload_color == "") {
        reload_color = "blue";
      }
      LoadCanvasTemplate_HTML = "<div><canvas id=\"canv\"></canvas><div><a id=\"reload_href\"  style=\"cursor: pointer; color: " + reload_color + "\">" + reload_text + "</a></div></div>";
      return ReactHtmlParser(LoadCanvasTemplate_HTML);
    }
  }]);
}(Component);
;
export var LoadCanvasTemplateNoReload = /*#__PURE__*/function (_Component2) {
  function LoadCanvasTemplateNoReload() {
    _classCallCheck(this, LoadCanvasTemplateNoReload);
    return _callSuper(this, LoadCanvasTemplateNoReload, arguments);
  }
  _inherits(LoadCanvasTemplateNoReload, _Component2);
  return _createClass(LoadCanvasTemplateNoReload, [{
    key: "render",
    value: function render() {
      return ReactHtmlParser(LoadCanvasTemplateNoReload_HTML);
    }
  }]);
}(Component);
;