"use strict";

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

var _preact = require("preact");

var _classnames = _interopRequireDefault(require("classnames"));

var _milliseconds = _interopRequireDefault(require("milliseconds"));

var _icon = require("./icon");

var _date = require("./utils/date");

var _bpm = require("./utils/bpm");

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

const maxAge = _milliseconds.default.seconds(60);

const delayedAge = _milliseconds.default.seconds(20);

const veryDelayedAge = _milliseconds.default.seconds(30);

const VitalEntry = ({
  name,
  value,
  unit,
  loading,
  error
}) => {
  let content;

  if (value == null && !error) {
    error = true;
  }

  if (loading) {
    content = (0, _preact.h)(_icon.Icon, {
      className: "spin-animation",
      size: "16",
      icon: "autorenew"
    });
  } else if (error) {
    content = (0, _preact.h)("div", {
      "data-e2e": "error",
      className: "flex items-center"
    }, (0, _preact.h)(_icon.Icon, {
      size: "16",
      icon: "error"
    }), error !== true ? (0, _preact.h)("span", {
      className: "ml1"
    }, error) : null);
  } else {
    content = (0, _preact.h)("strong", {
      "data-e2e": "value"
    }, value);
  }

  return (0, _preact.h)("div", {
    "data-e2e": "vital|".concat(name),
    className: (0, _classnames.default)('f6 fw4 flex items-center mr1', {
      'bg-red white br1 pa1': error
    })
  }, (0, _preact.h)("span", {
    className: "mr1"
  }, name, ":"), content, unit);
};

class ConnectedMonitorStatus extends _preact.Component {
  constructor(props) {
    super(props);
    this.state = {
      now: Date.now()
    };
    this.refreshTimer = null;
  }

  startTimer() {
    if (!this.refreshTimer) {
      this.refreshTimer = setInterval(() => this.setState({
        now: Date.now()
      }), 1000);
    }
  }

  render() {
    const {
      latestMetadata,
      isLocal,
      extraContent,
      className,
      hideLeadingIcon = false
    } = this.props;
    const {
      now
    } = this.state;

    if (!latestMetadata || !latestMetadata.connectionId) {
      clearTimeout(this.refreshTimer);
      this.refreshTimer = null;
      return null;
    }

    const {
      time,
      bpTime,
      vitals,
      hasValues,
      simulated,
      monitorLabel,
      autoVitalsStatus
    } = latestMetadata;
    const age = now - time;

    if (age > maxAge && !isLocal) {
      return null;
    }

    this.startTimer();
    let isVeryDelayed = !isLocal ? age > veryDelayedAge : false;
    let isSomewhatDelayed = !isLocal && !isVeryDelayed ? age > delayedAge : false;
    const isConnected = autoVitalsStatus === 'connected';
    const isWaiting = autoVitalsStatus === 'waiting';
    const hr = vitals.find(item => item.id === 'hr');
    return (0, _preact.h)("div", {
      "data-e2e": "connectedMonitorVitals",
      className: (0, _classnames.default)('flex items-center flex-wrap justify-between', className)
    }, (0, _preact.h)("div", {
      className: "flex items-center"
    }, !hideLeadingIcon && (0, _preact.h)("div", null, isConnected && (0, _preact.h)(_icon.Icon, {
      className: "nl1 ".concat(isLocal ? 'dark-blue' : 'light-blue'),
      size: "48",
      icon: "live_dot",
      speed: (0, _bpm.bpmToPlaybackSpeed)(hr && hr.value),
      "data-e2e": "connectedMonitorConnected"
    }), isWaiting && (0, _preact.h)(_icon.Icon, {
      "data-e2e": "connectedMonitorConnecting",
      size: "24",
      icon: "autorenew",
      className: "spin-animation"
    })), (0, _preact.h)("div", {
      className: "pa1"
    }, (0, _preact.h)("div", {
      className: "flex items-center flex-wrap"
    }, (0, _preact.h)("div", null, (0, _preact.h)("div", {
      className: "flex items-center fw6 nowrap"
    }, (0, _preact.h)("span", {
      className: "mr1"
    }, monitorLabel, " Data"), simulated && (0, _preact.h)("span", {
      className: "mr1"
    }, "(simulated)"), !isLocal && (0, _preact.h)("span", {
      className: "mr1"
    }, "(via other computer)"))), !isWaiting && (0, _preact.h)("div", {
      className: (0, _classnames.default)('nowrap mr1', {
        orange: isSomewhatDelayed,
        red: isVeryDelayed
      })
    }, "Received: ", time ? "".concat((0, _date.msToTimeAgo)(now - time), " ago") : '(none)', ","), !isWaiting && (0, _preact.h)("div", {
      className: "nowrap mr1"
    }, "Last BP received:", ' ', typeof bpTime === 'number' ? "".concat((0, _date.msToTimeAgo)(now - bpTime), " ago") : '(none)')), !isWaiting && (0, _preact.h)("div", null, (0, _preact.h)("div", {
      className: "flex flex-wrap mt1"
    }, vitals.map(vital => (0, _preact.h)(VitalEntry, {
      key: vital.id,
      name: vital.chartName,
      value: vital.value,
      error: vital.error,
      loading: vital.status === 'measuring'
    })))), !hasValues && isConnected && (0, _preact.h)("div", {
      className: "inline-flex items-center mt1"
    }, (0, _preact.h)(_icon.Icon, {
      icon: "warning",
      size: "16",
      className: "mr1"
    }), (0, _preact.h)("span", null, "The monitor is connected but sending blank vitals values.")))), extraContent, !isLocal && (isVeryDelayed || isSomewhatDelayed) && (0, _preact.h)("div", {
      className: (0, _classnames.default)('br2 pa2 flex items-center', {
        'bg-red white': isVeryDelayed,
        'bg-orange': isSomewhatDelayed
      })
    }, (0, _preact.h)(_icon.Icon, {
      icon: "warning",
      className: "mr2"
    }), "Data is delayed"));
  }

}

exports.ConnectedMonitorStatus = ConnectedMonitorStatus;