"use strict";

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

var _preact = require("preact");

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

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

function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }

function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }

function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }

const nameMap = {
  minimal: 'Minimal',
  moderate: 'Moderate',
  deep: 'Deep',
  ga: 'GA'
};
const levelMap = ['ga', 'deep', 'moderate', 'minimal'];
const relativeWeights = [5, 4, 3, 2];
const totalWeight = relativeWeights.reduce((a, b) => a + b, 0);
const weightsAsPercentage = relativeWeights.map(weight => weight / totalWeight);

const calculateSizes = totalAvailableSize => {
  const availableToSplit = totalAvailableSize - 3;
  let total = 0;
  const levels = levelMap.reduce((res, _, index) => {
    const weightedAmount = Math.floor(availableToSplit * weightsAsPercentage[index]);
    res.push(weightedAmount);
    total += weightedAmount;
    return res;
  }, []);
  let count = 0;

  while (total !== availableToSplit) {
    levels[count] += 1;
    count++;
    total++;
  }

  return levels;
};
/** @typedef {'minimal' | 'moderate' | 'deep' | 'ga'} SedationLevel */

/**
 * Small wrapped component for showing a color-coded FHIR appointment status.
 *
 * @typedef {import('preact').FunctionComponent} Appointment
 * @param {{
 *   sedationLevel: SedationLevel
 *   className?: string
 *   emptyColor?: string
 *   size?: number
 * }} Props
 */


const SedationLevel = (_ref) => {
  let {
    sedationLevel,
    size = 18,
    className,
    emptyColor = 'light-gray'
  } = _ref,
      rest = _objectWithoutProperties(_ref, ["sedationLevel", "size", "className", "emptyColor"]);

  const indexOfMatch = levelMap.indexOf(sedationLevel);
  const heightByIndex = calculateSizes(size);
  const isNone = indexOfMatch === -1;
  const width = size + 'px';
  return (0, _preact.h)("span", _extends({
    className: (0, _classnames.default)('inline-flex items-center gap2', className)
  }, rest), sedationLevel ? nameMap[sedationLevel] : '(not set)', ' ', (0, _preact.h)("div", {
    style: {
      width,
      height: size + 'px'
    }
  }, heightByIndex.map((height, index) => {
    const bgColor = isNone || indexOfMatch > index ? 'bg-' + emptyColor : ['bg-red', 'bg-orange', 'bg-gold', 'bg-yellow'][index];
    return (0, _preact.h)("div", {
      key: index,
      className: bgColor,
      style: {
        width,
        height: height + 'px',
        borderRadius: '1px',
        marginBottom: index < 3 ? '1px' : '0'
      }
    });
  })));
};

exports.SedationLevel = SedationLevel;