"use strict";

exports.__esModule = true;
exports["default"] = void 0;

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

var _chromaJs = _interopRequireDefault(require("chroma-js"));

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

function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; subClass.__proto__ = superClass; }

var approxeq = function approxeq(v1, v2, epsilon) {
  if (epsilon === void 0) {
    epsilon = 0.001;
  }

  return Math.abs(v1 - v2) <= epsilon;
};

var AbsoluteLegend = /*#__PURE__*/function (_React$Component) {
  _inheritsLoose(AbsoluteLegend, _React$Component);

  function AbsoluteLegend() {
    return _React$Component.apply(this, arguments) || this;
  }

  var _proto = AbsoluteLegend.prototype;

  _proto.render = function render() {
    var _this = this;

    var labels = [];
    var items = [];

    if (typeof this.props.colorScale == "function") {
      var step = (this.props.limits[1] - this.props.limits[0]) / this.props.steps;

      for (var i = this.props.limits[0]; i <= this.props.limits[1]; i += step) {
        var iStr = i.toLocaleString();

        if (approxeq(i, this.props.limits[0])) {
          iStr = "<=" + iStr;
        } else if (approxeq(i, this.props.limits[1])) {
          iStr = ">=" + iStr;
        }

        items.push( /*#__PURE__*/_react["default"].createElement("div", {
          key: i
        }, /*#__PURE__*/_react["default"].createElement("i", {
          style: {
            background: this.props.colorScale(i),
            width: "18px",
            height: "18px",
            marginRight: "5px"
          }
        }), iStr));
      }
    } else if (typeof (this.props.colorScale == "object") && this.props.height) {
      // Bivariate, second variable as height
      var _step = (this.props.limits[0][1] - this.props.limits[0][0]) / this.props.steps;

      for (var i = this.props.limits[0][0]; i <= this.props.limits[0][1]; i += _step) {
        var iStr = i.toLocaleString();

        if (approxeq(i, this.props.limits[0][0])) {
          iStr = "<=" + iStr;
        } else if (approxeq(i, this.props.limits[0][1])) {
          iStr = ">=" + iStr;
        }

        items.push( /*#__PURE__*/_react["default"].createElement("div", {
          key: i
        }, /*#__PURE__*/_react["default"].createElement("i", {
          style: {
            background: this.props.colorScale[0](i),
            width: "18px",
            height: "18px",
            marginRight: "5px"
          }
        }), iStr));
      }
    } else if (typeof (this.props.colorScale == "object")) {
      // Bivariate chloropleth
      var dx = (this.props.limits[0][1] - this.props.limits[0][0]) / this.props.steps;
      var dy = (this.props.limits[1][1] - this.props.limits[1][0]) / this.props.steps;
      labels.push( /*#__PURE__*/_react["default"].createElement("div", {
        key: "label0"
      }, this.props.labels[0]));
      labels.push( /*#__PURE__*/_react["default"].createElement("div", {
        key: "label1",
        style: {
          transform: "rotate(90deg) translate(160px, 30px)"
        }
      }, this.props.labels[1]));

      for (var iy = 0; iy < this.props.steps; iy++) {
        for (var ix = 0; ix < this.props.steps; ix++) {
          var x = this.props.limits[0][0] + ix * dx;
          var y = this.props.limits[1][0] + iy * dy;
          items.push( /*#__PURE__*/_react["default"].createElement("i", {
            key: ix + "_" + iy,
            style: {
              background: _chromaJs["default"].blend(this.props.colorScale[0](x), this.props.colorScale[1](y), "multiply")
            },
            title: x.toLocaleString() + "," + y.toLocaleString()
          }));
        }
      }

      items = /*#__PURE__*/_react["default"].createElement("div", {
        id: "grid",
        style: {
          gridTemplateColumns: "repeat(" + this.props.steps + ", 18px)"
        }
      }, items);
    }

    var select = "";

    if (typeof this.props.colorBy == "object") {
      var options = [];

      for (var k in this.props.colorBy) {
        options.push( /*#__PURE__*/_react["default"].createElement("option", {
          key: k
        }, k));
      }

      select = /*#__PURE__*/_react["default"].createElement("div", null, "Colour by: ", /*#__PURE__*/_react["default"].createElement("select", {
        value: this.props.accessorName,
        onChange: function onChange(e) {
          return _this.props.changeAccessor(e.target.value);
        }
      }, options));
    }

    return /*#__PURE__*/_react["default"].createElement("div", {
      key: "legend",
      className: "legend",
      style: {
        right: this.props.right || 30,
        bottom: this.props.bottom || 30
      }
    }, /*#__PURE__*/_react["default"].createElement("h4", null, this.props.title), labels, items, "Hover over an area to show plots for that area", select, /*#__PURE__*/_react["default"].createElement("div", {
      id: "heightWrapper"
    }, /*#__PURE__*/_react["default"].createElement("input", {
      name: "height",
      type: "checkbox",
      checked: this.props.height,
      onChange: this.props.toggleHeight,
      disabled: typeof this.props.colorScale !== "object"
    }), /*#__PURE__*/_react["default"].createElement("label", {
      htmlFor: "height"
    }, "second variable as height")));
  };

  return AbsoluteLegend;
}(_react["default"].Component);

exports["default"] = AbsoluteLegend;
module.exports = exports.default;