"use strict";

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

var _typeof = require("@babel/runtime/helpers/typeof");

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));

var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));

var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));

var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));

var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));

var _assertThisInitialized2 = _interopRequireDefault(require("@babel/runtime/helpers/assertThisInitialized"));

var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits"));

var _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn"));

var _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf"));

var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));

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

var _Button = _interopRequireDefault(require("../../components/Button"));

var _TextInput = _interopRequireDefault(require("../../components/TextInput"));

var _Module = require("../../components/Module");

var _ReduxFormWrapper = _interopRequireDefault(require("../../components/ReduxFormWrapper"));

var _reactFinalForm = require("react-final-form");

var _favicon = require("./favicon/favicon.js");

var _center = _interopRequireDefault(require("./favicon/center"));

var _colors = _interopRequireDefault(require("../../globals/data/colors.js"));

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = (0, _getPrototypeOf2.default)(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = (0, _getPrototypeOf2.default)(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return (0, _possibleConstructorReturn2.default)(this, result); }; }

function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }

var canvas = {
  width: 128,
  height: 128
};

var FaviconCanvas = /*#__PURE__*/function (_React$Component) {
  (0, _inherits2.default)(FaviconCanvas, _React$Component);

  var _super = _createSuper(FaviconCanvas);

  function FaviconCanvas() {
    var _this;

    (0, _classCallCheck2.default)(this, FaviconCanvas);

    for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }

    _this = _super.call.apply(_super, [this].concat(args));
    (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "download", function () {
      var canvas = document.getElementById('canvas');

      var dataurl = _favicon.Package.generate(canvas); //const dataurl = ico.generate([16, 32, 48]);


      _this.props.updateUrl(dataurl);
    });
    return _this;
  }

  (0, _createClass2.default)(FaviconCanvas, [{
    key: "componentDidMount",
    value: function componentDidMount() {
      this.updateCanvas();
    }
  }, {
    key: "componentDidUpdate",
    value: function componentDidUpdate() {
      this.updateCanvas();
    }
  }, {
    key: "updateCanvas",
    value: function updateCanvas() {
      var context = this.refs.canvas.getContext('2d');
      context.clearRect(0, 0, 300, 300);
      var mainColor = _colors.default.main.hex;
      var name = this.props.name && this.props.name.length >= 1 ? this.props.name : '–';
      (0, _center.default)({
        canvas: this.refs.canvas,
        width: canvas.width,
        height: canvas.height,
        shape: 'square',
        fontColor: '#ffffff',
        backgroundColor: mainColor,
        text: name.toUpperCase(),
        fontFamily: 'Open Sans',
        fontWeight: 'bold',
        fontSize: 15 + 120 * 1 / name.length
      });
    }
  }, {
    key: "render",
    value: function render() {
      return /*#__PURE__*/_react.default.createElement("canvas", {
        id: "canvas",
        ref: "canvas",
        width: 128,
        height: 128
      });
    }
  }]);
  return FaviconCanvas;
}(_react.default.Component);

var onSubmit = /*#__PURE__*/function () {
  var _ref = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee(values) {
    return _regenerator.default.wrap(function _callee$(_context) {
      while (1) {
        switch (_context.prev = _context.next) {
          case 0:
            window.alert(JSON.stringify(values, 0, 2));

          case 1:
          case "end":
            return _context.stop();
        }
      }
    }, _callee);
  }));

  return function onSubmit(_x) {
    return _ref.apply(this, arguments);
  };
}();

var FaviconGenerator = /*#__PURE__*/function (_Component) {
  (0, _inherits2.default)(FaviconGenerator, _Component);

  var _super2 = _createSuper(FaviconGenerator);

  function FaviconGenerator(props) {
    var _this2;

    (0, _classCallCheck2.default)(this, FaviconGenerator);
    _this2 = _super2.call(this, props);
    (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this2), "onClick", function () {
      _this2.child.current.download();
    });
    (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this2), "updateUrl", function (dataurl) {
      _this2.setState({
        dataurl: dataurl
      });
    });
    _this2.child = /*#__PURE__*/_react.default.createRef();
    _this2.state = {
      dataurl: {}
    };
    return _this2;
  }

  (0, _createClass2.default)(FaviconGenerator, [{
    key: "render",
    value: function render() {
      var _this3 = this;

      var buttonElements = Object.keys(this.state.dataurl).map(function (file, i) {
        return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement(_Button.default, {
          download: "wfp-favicon-".concat(file, ".").concat(file.substring(0, 3)),
          kind: "secondary",
          small: true,
          href: _this3.state.dataurl[file]
        }, file), ' ');
      });
      return /*#__PURE__*/_react.default.createElement("div", null, /*#__PURE__*/_react.default.createElement(_reactFinalForm.Form, {
        onSubmit: onSubmit,
        initialValues: {
          name: 'WFP'
        },
        render: function render(_ref2) {
          var handleSubmit = _ref2.handleSubmit,
              form = _ref2.form,
              submitting = _ref2.submitting,
              pristine = _ref2.pristine,
              values = _ref2.values;
          return /*#__PURE__*/_react.default.createElement("form", {
            onSubmit: handleSubmit
          }, /*#__PURE__*/_react.default.createElement(_Module.Module, {
            light: true,
            className: "some-class",
            style: {
              padding: '1em 0'
            }
          }, /*#__PURE__*/_react.default.createElement(_Module.ModuleBody, null, /*#__PURE__*/_react.default.createElement("div", {
            style: {
              display: 'flex',
              justifyContent: 'space-between'
            }
          }, /*#__PURE__*/_react.default.createElement("div", {
            className: "wfp--form-long"
          }, /*#__PURE__*/_react.default.createElement(_reactFinalForm.Field, {
            name: "name",
            component: _ReduxFormWrapper.default,
            inputComponent: _TextInput.default,
            type: "text",
            labelText: "Acronym",
            helperText: "Abbreviation of the applications name, like: WSS, WFP, INFO",
            placeholder: "WFP"
          }), /*#__PURE__*/_react.default.createElement("div", {
            className: "wfp--form-item"
          }, /*#__PURE__*/_react.default.createElement(_Button.default, {
            onClick: _this3.onClick
          }, "Generate files")), /*#__PURE__*/_react.default.createElement("div", {
            style: {
              marginBottom: '1rem'
            }
          }, Object.keys(_this3.state.dataurl).length > 0 && /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement("h4", null, "Download"), buttonElements))), /*#__PURE__*/_react.default.createElement("div", null, /*#__PURE__*/_react.default.createElement("label", {
            className: "wfp--label"
          }, "Preview"), /*#__PURE__*/_react.default.createElement("br", null), /*#__PURE__*/_react.default.createElement(FaviconCanvas, (0, _extends2.default)({
            updateUrl: _this3.updateUrl
          }, values, {
            ref: _this3.child
          })))))));
        }
      }));
    }
  }]);
  return FaviconGenerator;
}(_react.Component);

exports.default = FaviconGenerator;