"use strict";

exports.__esModule = true;
exports.default = void 0;

var _ssrWindow = require("ssr-window");

var _dom = _interopRequireDefault(require("../../shared/dom7"));

var _utils = require("../../shared/utils");

var _class = _interopRequireDefault(require("../../shared/class"));

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

function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }

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

var Gauge = /*#__PURE__*/function (_Framework7Class) {
  _inheritsLoose(Gauge, _Framework7Class);

  function Gauge(app, params) {
    var _this;

    if (params === void 0) {
      params = {};
    }

    // Extends with open/close Modal methods;
    _this = _Framework7Class.call(this, params, [app]) || this;

    var gauge = _assertThisInitialized(_this);

    var defaults = (0, _utils.extend)({}, app.params.gauge); // Extend defaults with modules params

    gauge.useModulesParams(defaults);
    gauge.params = (0, _utils.extend)(defaults, params);
    var el = gauge.params.el;
    if (!el) return gauge || _assertThisInitialized(_this);
    var $el = (0, _dom.default)(el);
    if ($el.length === 0) return gauge || _assertThisInitialized(_this);
    if ($el[0].f7Gauge) return $el[0].f7Gauge || _assertThisInitialized(_this);
    (0, _utils.extend)(gauge, {
      app: app,
      $el: $el,
      el: $el && $el[0]
    });
    $el[0].f7Gauge = gauge; // Install Modules

    gauge.useModules();
    gauge.init();
    return gauge || _assertThisInitialized(_this);
  }

  var _proto = Gauge.prototype;

  _proto.calcRadius = function calcRadius() {
    var gauge = this;
    var _gauge$params = gauge.params,
        size = _gauge$params.size,
        borderWidth = _gauge$params.borderWidth;
    return size / 2 - borderWidth / 2;
  };

  _proto.calcBorderLength = function calcBorderLength() {
    var gauge = this;
    var radius = gauge.calcRadius();
    return 2 * Math.PI * radius;
  };

  _proto.render = function render() {
    var gauge = this;
    if (gauge.params.render) return gauge.params.render.call(gauge, gauge);
    var _gauge$params2 = gauge.params,
        type = _gauge$params2.type,
        value = _gauge$params2.value,
        size = _gauge$params2.size,
        bgColor = _gauge$params2.bgColor,
        borderBgColor = _gauge$params2.borderBgColor,
        borderColor = _gauge$params2.borderColor,
        borderWidth = _gauge$params2.borderWidth,
        valueText = _gauge$params2.valueText,
        valueTextColor = _gauge$params2.valueTextColor,
        valueFontSize = _gauge$params2.valueFontSize,
        valueFontWeight = _gauge$params2.valueFontWeight,
        labelText = _gauge$params2.labelText,
        labelTextColor = _gauge$params2.labelTextColor,
        labelFontSize = _gauge$params2.labelFontSize,
        labelFontWeight = _gauge$params2.labelFontWeight;
    var semiCircle = type === 'semicircle';
    var radius = gauge.calcRadius();
    var length = gauge.calcBorderLength();
    var progress = Math.max(Math.min(value, 1), 0); // prettier-ignore

    return ("\n      <svg class=\"gauge-svg\" width=\"" + size + "px\" height=\"" + (semiCircle ? size / 2 : size) + "px\" viewBox=\"0 0 " + size + " " + (semiCircle ? size / 2 : size) + "\">\n        " + (semiCircle ? "\n          <path\n            class=\"gauge-back-semi\"\n            d=\"M" + (size - borderWidth / 2) + "," + size / 2 + " a1,1 0 0,0 -" + (size - borderWidth) + ",0\"\n            stroke=\"" + borderBgColor + "\"\n            stroke-width=\"" + borderWidth + "\"\n            fill=\"" + (bgColor || 'none') + "\"\n          />\n          <path\n            class=\"gauge-front-semi\"\n            d=\"M" + (size - borderWidth / 2) + "," + size / 2 + " a1,1 0 0,0 -" + (size - borderWidth) + ",0\"\n            stroke=\"" + borderColor + "\"\n            stroke-width=\"" + borderWidth + "\"\n            stroke-dasharray=\"" + length / 2 + "\"\n            stroke-dashoffset=\"" + length / 2 * (1 + progress) + "\"\n            fill=\"" + (borderBgColor ? 'none' : bgColor || 'none') + "\"\n          />\n        " : "\n          " + (borderBgColor ? "\n            <circle\n              class=\"gauge-back-circle\"\n              stroke=\"" + borderBgColor + "\"\n              stroke-width=\"" + borderWidth + "\"\n              fill=\"" + (bgColor || 'none') + "\"\n              cx=\"" + size / 2 + "\"\n              cy=\"" + size / 2 + "\"\n              r=\"" + radius + "\"\n            ></circle>\n          " : '') + "\n          <circle\n            class=\"gauge-front-circle\"\n            transform=\"" + ("rotate(-90 " + size / 2 + " " + size / 2 + ")") + "\"\n            stroke=\"" + borderColor + "\"\n            stroke-width=\"" + borderWidth + "\"\n            stroke-dasharray=\"" + length + "\"\n            stroke-dashoffset=\"" + length * (1 - progress) + "\"\n            fill=\"" + (borderBgColor ? 'none' : bgColor || 'none') + "\"\n            cx=\"" + size / 2 + "\"\n            cy=\"" + size / 2 + "\"\n            r=\"" + radius + "\"\n          ></circle>\n        ") + "\n        " + (valueText ? "\n          <text\n            class=\"gauge-value-text\"\n            x=\"50%\"\n            y=\"" + (semiCircle ? '100%' : '50%') + "\"\n            font-weight=\"" + valueFontWeight + "\"\n            font-size=\"" + valueFontSize + "\"\n            fill=\"" + valueTextColor + "\"\n            dy=\"" + (semiCircle ? labelText ? -labelFontSize - 15 : -5 : 0) + "\"\n            text-anchor=\"middle\"\n            dominant-baseline=\"" + (!semiCircle && 'middle') + "\"\n          >" + valueText + "</text>\n        " : '') + "\n        " + (labelText ? "\n          <text\n            class=\"gauge-label-text\"\n            x=\"50%\"\n            y=\"" + (semiCircle ? '100%' : '50%') + "\"\n            font-weight=\"" + labelFontWeight + "\"\n            font-size=\"" + labelFontSize + "\"\n            fill=\"" + labelTextColor + "\"\n            dy=\"" + (semiCircle ? -5 : valueText ? valueFontSize / 2 + 10 : 0) + "\"\n            text-anchor=\"middle\"\n            dominant-baseline=\"" + (!semiCircle && 'middle') + "\"\n          >" + labelText + "</text>\n        " : '') + "\n      </svg>\n    ").trim();
  };

  _proto.update = function update(newParams) {
    if (newParams === void 0) {
      newParams = {};
    }

    var gauge = this;
    var document = (0, _ssrWindow.getDocument)();
    var params = gauge.params,
        $gaugeSvgEl = gauge.$gaugeSvgEl;
    Object.keys(newParams).forEach(function (param) {
      if (typeof newParams[param] !== 'undefined') {
        params[param] = newParams[param];
      }
    });
    if ($gaugeSvgEl.length === 0) return gauge;
    var value = params.value,
        size = params.size,
        bgColor = params.bgColor,
        borderBgColor = params.borderBgColor,
        borderColor = params.borderColor,
        borderWidth = params.borderWidth,
        valueText = params.valueText,
        valueTextColor = params.valueTextColor,
        valueFontSize = params.valueFontSize,
        valueFontWeight = params.valueFontWeight,
        labelText = params.labelText,
        labelTextColor = params.labelTextColor,
        labelFontSize = params.labelFontSize,
        labelFontWeight = params.labelFontWeight;
    var length = gauge.calcBorderLength();
    var progress = Math.max(Math.min(value, 1), 0);
    var radius = gauge.calcRadius();
    var semiCircle = params.type === 'semicircle';
    var svgAttrs = {
      width: size + "px",
      height: (semiCircle ? size / 2 : size) + "px",
      viewBox: "0 0 " + size + " " + (semiCircle ? size / 2 : size)
    };
    Object.keys(svgAttrs).forEach(function (attr) {
      $gaugeSvgEl.attr(attr, svgAttrs[attr]);
    });

    if (semiCircle) {
      var backAttrs = {
        d: "M" + (size - borderWidth / 2) + "," + size / 2 + " a1,1 0 0,0 -" + (size - borderWidth) + ",0",
        stroke: borderBgColor,
        'stroke-width': borderWidth,
        fill: bgColor || 'none'
      };
      var frontAttrs = {
        d: "M" + (size - borderWidth / 2) + "," + size / 2 + " a1,1 0 0,0 -" + (size - borderWidth) + ",0",
        stroke: borderColor,
        'stroke-width': borderWidth,
        'stroke-dasharray': length / 2,
        'stroke-dashoffset': length / 2 * (1 + progress),
        fill: borderBgColor ? 'none' : bgColor || 'none'
      };
      Object.keys(backAttrs).forEach(function (attr) {
        $gaugeSvgEl.find('.gauge-back-semi').attr(attr, backAttrs[attr]);
      });
      Object.keys(frontAttrs).forEach(function (attr) {
        $gaugeSvgEl.find('.gauge-front-semi').attr(attr, frontAttrs[attr]);
      });
    } else {
      var _backAttrs = {
        stroke: borderBgColor,
        'stroke-width': borderWidth,
        fill: bgColor || 'none',
        cx: size / 2,
        cy: size / 2,
        r: radius
      };
      var _frontAttrs = {
        transform: "rotate(-90 " + size / 2 + " " + size / 2 + ")",
        stroke: borderColor,
        'stroke-width': borderWidth,
        'stroke-dasharray': length,
        'stroke-dashoffset': length * (1 - progress),
        fill: borderBgColor ? 'none' : bgColor || 'none',
        cx: size / 2,
        cy: size / 2,
        r: radius
      };
      Object.keys(_backAttrs).forEach(function (attr) {
        $gaugeSvgEl.find('.gauge-back-circle').attr(attr, _backAttrs[attr]);
      });
      Object.keys(_frontAttrs).forEach(function (attr) {
        $gaugeSvgEl.find('.gauge-front-circle').attr(attr, _frontAttrs[attr]);
      });
    }

    if (valueText) {
      if (!$gaugeSvgEl.find('.gauge-value-text').length) {
        var textEl = document.createElementNS('http://www.w3.org/2000/svg', 'text');
        textEl.classList.add('gauge-value-text');
        $gaugeSvgEl.append(textEl);
      }

      var textAttrs = {
        x: '50%',
        y: semiCircle ? '100%' : '50%',
        'font-weight': valueFontWeight,
        'font-size': valueFontSize,
        fill: valueTextColor,
        dy: semiCircle ? labelText ? -labelFontSize - 15 : -5 : 0,
        'text-anchor': 'middle',
        'dominant-baseline': !semiCircle && 'middle'
      };
      Object.keys(textAttrs).forEach(function (attr) {
        $gaugeSvgEl.find('.gauge-value-text').attr(attr, textAttrs[attr]);
      });
      $gaugeSvgEl.find('.gauge-value-text').text(valueText);
    } else {
      $gaugeSvgEl.find('.gauge-value-text').remove();
    }

    if (labelText) {
      if (!$gaugeSvgEl.find('.gauge-label-text').length) {
        var _textEl = document.createElementNS('http://www.w3.org/2000/svg', 'text');

        _textEl.classList.add('gauge-label-text');

        $gaugeSvgEl.append(_textEl);
      }

      var labelAttrs = {
        x: '50%',
        y: semiCircle ? '100%' : '50%',
        'font-weight': labelFontWeight,
        'font-size': labelFontSize,
        fill: labelTextColor,
        dy: semiCircle ? -5 : valueText ? valueFontSize / 2 + 10 : 0,
        'text-anchor': 'middle',
        'dominant-baseline': !semiCircle && 'middle'
      };
      Object.keys(labelAttrs).forEach(function (attr) {
        $gaugeSvgEl.find('.gauge-label-text').attr(attr, labelAttrs[attr]);
      });
      $gaugeSvgEl.find('.gauge-label-text').text(labelText);
    } else {
      $gaugeSvgEl.find('.gauge-label-text').remove();
    }

    return gauge;
  };

  _proto.init = function init() {
    var gauge = this;
    var $gaugeSvgEl = (0, _dom.default)(gauge.render()).eq(0);
    $gaugeSvgEl.f7Gauge = gauge;
    (0, _utils.extend)(gauge, {
      $gaugeSvgEl: $gaugeSvgEl,
      gaugeSvgEl: $gaugeSvgEl && $gaugeSvgEl[0]
    });
    gauge.$el.append($gaugeSvgEl);
    return gauge;
  };

  _proto.destroy = function destroy() {
    var gauge = this;
    if (!gauge.$el || gauge.destroyed) return;
    gauge.$el.trigger('gauge:beforedestroy');
    gauge.emit('local::beforeDestroy gaugeBeforeDestroy', gauge);
    gauge.$gaugeSvgEl.remove();
    delete gauge.$el[0].f7Gauge;
    (0, _utils.deleteProps)(gauge);
    gauge.destroyed = true;
  };

  return Gauge;
}(_class.default);

var _default = Gauge;
exports.default = _default;