'use strict';

var _defineProperty = require('@babel/runtime/helpers/defineProperty');
var classnames = require('classnames');
var Warning = require('@bigbinary/neeto-icons/Warning');
var Tooltip = require('@bigbinary/neetoui/Tooltip');
var Typography = require('@bigbinary/neetoui/Typography');
var Spinner = require('@bigbinary/neetoui/Spinner');
var reactI18next = require('react-i18next');
var DateRangeFilter = require('./DateRangeFilter.js');
var i18next = require('i18next');
var React = require('react');
var ramda = require('ramda');
var jsxRuntime = require('react/jsx-runtime');
var neetoCist = require('@bigbinary/neeto-cist');
var HelpPopover = require('./HelpPopover.js');
require('@babel/runtime/helpers/slicedToArray');
require('@bigbinary/neeto-commons-frontend/initializers');
require('@bigbinary/neeto-commons-frontend/utils');
require('@bigbinary/neetoui/Select');
require('@bigbinary/neetoui/DatePicker');
require('@babel/runtime/helpers/objectWithoutProperties');
require('@bigbinary/neeto-icons/Help');
require('@bigbinary/neetoui/Button');
require('@bigbinary/neetoui/Popover');

var DEFAULT_DEVICES = [{
  label: i18next.t("neetoMolecules.insights.labels.devices.desktop"),
  value: "desktop"
}, {
  label: i18next.t("neetoMolecules.insights.labels.devices.mobile"),
  value: "smartphone"
}];
var DEFAULT_OS = [{
  label: i18next.t("neetoMolecules.insights.labels.os.macos"),
  value: "mac"
}, {
  label: i18next.t("neetoMolecules.insights.labels.os.windows"),
  value: "windows"
}];
var DEFAULT_BROWSERS = [{
  label: i18next.t("neetoMolecules.insights.labels.browsers.chrome"),
  value: "chrome"
}, {
  label: i18next.t("neetoMolecules.insights.labels.browsers.safari"),
  value: "safari"
}];
({
  label: i18next.t("neetoMolecules.insights.labels.allTime")});

var PercentageBar = function PercentageBar(_ref) {
  var value = _ref.value;
  return /*#__PURE__*/jsxRuntime.jsxs(jsxRuntime.Fragment, {
    children: [value !== 0 && /*#__PURE__*/jsxRuntime.jsx("div", {
      className: "neeto-ui-bg-gray-300 neeto-ui-rounded-full h-2",
      style: {
        width: "".concat(value, "%")
      }
    }), /*#__PURE__*/jsxRuntime.jsx(Typography, {
      style: "body3",
      children: value ? "".concat(value, "%") : 0
    })]
  });
};
var PercentageBar$1 = /*#__PURE__*/React.memo(PercentageBar);

var MetaInfoItem = function MetaInfoItem(_ref) {
  var name = _ref.name,
    value = _ref.value;
  return /*#__PURE__*/jsxRuntime.jsxs(jsxRuntime.Fragment, {
    children: [/*#__PURE__*/jsxRuntime.jsx(Typography, {
      style: "body2",
      textTransform: "capitalize",
      children: name
    }), /*#__PURE__*/jsxRuntime.jsx("div", {
      className: "col-span-3 flex items-center gap-2",
      children: /*#__PURE__*/jsxRuntime.jsx(PercentageBar$1, {
        value: value
      })
    })]
  });
};
var MetaInfoItem$1 = /*#__PURE__*/React.memo(MetaInfoItem);

var DataDistribution = function DataDistribution(_ref) {
  var data = _ref.data;
  return /*#__PURE__*/jsxRuntime.jsx("div", {
    className: "grid grid-cols-4 gap-2",
    children: data.map(function (_ref2) {
      var name = _ref2.name,
        value = _ref2.value;
      return /*#__PURE__*/React.createElement(MetaInfoItem$1, {
        name: name,
        value: value,
        key: name
      });
    })
  });
};

var getSum = function getSum(data, searchKey) {
  var sum = 0;
  for (var key in data) {
    if (key.includes(searchKey)) sum += data[key];
  }
  return sum;
};
var transformInsights = function transformInsights(data, defaultItems) {
  var othersData = Object.keys(data).filter(function (key) {
    return !neetoCist.existsBy({
      value: key
    }, defaultItems);
  }).map(function (key) {
    return {
      name: key,
      value: data[key]
    };
  });
  var defaultData = defaultItems.map(function (_ref) {
    var label = _ref.label,
      value = _ref.value;
    return {
      name: label,
      value: getSum(data, value) || 0
    };
  });
  return {
    defaultData: defaultData,
    othersData: othersData
  };
};

var MetaInfo = function MetaInfo(_ref) {
  var type = _ref.type,
    data = _ref.data,
    defaultItems = _ref.defaultItems;
  var _useTranslation = reactI18next.useTranslation(),
    t = _useTranslation.t;
  var _transformInsights = transformInsights(data, defaultItems),
    defaultData = _transformInsights.defaultData,
    othersData = _transformInsights.othersData;
  var othersSum = ramda.sum(ramda.values(ramda.pluck("value", othersData)));
  return /*#__PURE__*/jsxRuntime.jsxs("div", {
    className: "neeto-ui-bg-gray-100 neeto-ui-rounded p-4",
    children: [/*#__PURE__*/jsxRuntime.jsx(Typography, {
      className: "mb-2",
      style: "body2",
      textTransform: "uppercase",
      weight: "bold",
      children: type
    }), /*#__PURE__*/jsxRuntime.jsx(DataDistribution, {
      data: defaultData
    }), /*#__PURE__*/jsxRuntime.jsx(Tooltip, {
      content: /*#__PURE__*/jsxRuntime.jsx(DataDistribution, {
        data: othersData
      }),
      disabled: !othersSum,
      position: "bottom",
      children: /*#__PURE__*/jsxRuntime.jsx("div", {
        className: "mt-2 grid grid-cols-4 gap-2",
        children: /*#__PURE__*/jsxRuntime.jsx(MetaInfoItem$1, {
          name: t("neetoMolecules.insights.others"),
          value: othersSum
        })
      })
    })]
  });
};
var MetaInfo$1 = /*#__PURE__*/React.memo(MetaInfo);

var TimeItem = function TimeItem(_ref) {
  var value = _ref.value,
    suffix = _ref.suffix;
  return /*#__PURE__*/jsxRuntime.jsxs("div", {
    className: "flex items-end",
    "data-testid": "time-item",
    children: [/*#__PURE__*/jsxRuntime.jsx(Typography, {
      className: "mr-1",
      style: "h1",
      weight: "semibold",
      children: value
    }), /*#__PURE__*/jsxRuntime.jsx(Typography, {
      lineHeight: "loose",
      style: "body2",
      children: suffix
    })]
  });
};
var TimeItem$1 = /*#__PURE__*/React.memo(TimeItem);

var MetricItem = function MetricItem(_ref) {
  var metric = _ref.metric,
    value = _ref.value,
    helpText = _ref.helpText,
    isTime = _ref.isTime,
    isPercentage = _ref.isPercentage;
  var renderTime = function renderTime(value) {
    var hours = value.h,
      minutes = value.m,
      seconds = value.s;
    var showSeconds = !hours && !minutes;
    return /*#__PURE__*/jsxRuntime.jsxs("div", {
      className: "flex gap-2",
      children: [!!hours && /*#__PURE__*/jsxRuntime.jsx(TimeItem$1, {
        suffix: "H",
        value: hours
      }), !!minutes && /*#__PURE__*/jsxRuntime.jsx(TimeItem$1, {
        suffix: "M",
        value: minutes
      }), (!!seconds || showSeconds) && /*#__PURE__*/jsxRuntime.jsx(TimeItem$1, {
        suffix: "S",
        value: seconds
      })]
    });
  };
  return /*#__PURE__*/jsxRuntime.jsxs("div", {
    className: "nf-insights__metric-item neeto-ui-border-gray-200 border-b pb-4",
    "data-cy": "".concat(neetoCist.hyphenate(metric), "-metric"),
    children: [/*#__PURE__*/jsxRuntime.jsxs("div", {
      className: "flex items-center gap-2",
      children: [/*#__PURE__*/jsxRuntime.jsx("div", {
        className: "flex items-end gap-x-1",
        children: isTime ? renderTime(value) : /*#__PURE__*/jsxRuntime.jsxs(Typography, {
          className: "neeto-ui-text-gray-800",
          "data-cy": "insights-count",
          "data-testid": "insights-count",
          style: "h1",
          weight: "semibold",
          children: [value || 0, isPercentage ? "%" : ""]
        })
      }), helpText && /*#__PURE__*/jsxRuntime.jsx("div", {
        children: /*#__PURE__*/jsxRuntime.jsx(HelpPopover, {
          description: helpText
        })
      })]
    }), /*#__PURE__*/jsxRuntime.jsx(Typography, {
      className: "neeto-ui-text-gray-700",
      "data-cy": "insights-type",
      style: "body3",
      children: metric
    })]
  });
};
var MetricItem$1 = /*#__PURE__*/React.memo(MetricItem);

function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), true).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
var Insights = function Insights(_ref) {
  var insightsData = _ref.insightsData,
    isLoading = _ref.isLoading,
    timePeriod = _ref.timePeriod,
    setTimePeriod = _ref.setTimePeriod,
    _ref$helpTexts = _ref.helpTexts,
    helpTexts = _ref$helpTexts === void 0 ? {} : _ref$helpTexts,
    _ref$showWarningMessa = _ref.showWarningMessage,
    showWarningMessage = _ref$showWarningMessa === void 0 ? false : _ref$showWarningMessa,
    className = _ref.className,
    title = _ref.title,
    _ref$tooltipProps = _ref.tooltipProps,
    tooltipProps = _ref$tooltipProps === void 0 ? {} : _ref$tooltipProps;
  var _useTranslation = reactI18next.useTranslation(),
    t = _useTranslation.t;
  var visitCount = insightsData.visitCount,
    startCount = insightsData.startCount,
    submissionCount = insightsData.submissionCount,
    completionRate = insightsData.completionRate,
    averageSubmissionTime = insightsData.averageSubmissionTime,
    devices = insightsData.devices,
    operatingSystems = insightsData.operatingSystems,
    browsers = insightsData.browsers;
  if (isLoading) {
    return /*#__PURE__*/jsxRuntime.jsx("div", {
      className: "flex h-full w-full items-center justify-center",
      children: /*#__PURE__*/jsxRuntime.jsx(Spinner, {})
    });
  }
  return /*#__PURE__*/jsxRuntime.jsxs("div", {
    className: classnames("mx-auto pt-10", className),
    "data-cy": "insights",
    "data-testid": "insights",
    children: [/*#__PURE__*/jsxRuntime.jsxs("div", {
      className: "mb-10 flex w-full items-center",
      children: [/*#__PURE__*/jsxRuntime.jsx(Typography, {
        "data-cy": "submissions-insights-title",
        style: "h2",
        children: title || t("neetoMolecules.insights.title")
      }), showWarningMessage && /*#__PURE__*/jsxRuntime.jsx(Tooltip, _objectSpread(_objectSpread({
        content: t("neetoMolecules.insights.dataInaccuracyWarning"),
        position: "bottom",
        touch: ["hold", 500]
      }, tooltipProps), {}, {
        children: /*#__PURE__*/jsxRuntime.jsx("span", {
          className: "ml-3",
          children: /*#__PURE__*/jsxRuntime.jsx(Warning, {
            className: "neeto-ui-text-warning-500",
            "data-cy": "insights-warning",
            "data-testid": "insights-warning",
            size: 20
          })
        })
      })), /*#__PURE__*/jsxRuntime.jsx("span", {
        className: "ml-auto",
        children: /*#__PURE__*/jsxRuntime.jsx(DateRangeFilter, {
          setTimePeriod: setTimePeriod,
          timePeriod: timePeriod
        })
      })]
    }), /*#__PURE__*/jsxRuntime.jsxs("div", {
      className: "mb-6 grid grid-cols-1 gap-x-9 gap-y-6 sm:grid-cols-2  md:grid-cols-3 lg:grid-cols-5 lg:gap-0",
      children: [/*#__PURE__*/jsxRuntime.jsx(MetricItem$1, {
        helpText: helpTexts["visitCount"],
        metric: t("neetoMolecules.insights.metrics.visits"),
        value: visitCount
      }), /*#__PURE__*/jsxRuntime.jsx(MetricItem$1, {
        helpText: helpTexts["startCount"],
        metric: t("neetoMolecules.insights.metrics.starts"),
        value: startCount
      }), /*#__PURE__*/jsxRuntime.jsx(MetricItem$1, {
        helpText: helpTexts["submissions"],
        metric: t("neetoMolecules.insights.metrics.submissions"),
        value: submissionCount
      }), /*#__PURE__*/jsxRuntime.jsx(MetricItem$1, {
        isPercentage: true,
        helpText: helpTexts["completionRate"],
        metric: t("neetoMolecules.insights.metrics.completionRate"),
        value: completionRate
      }), /*#__PURE__*/jsxRuntime.jsx(MetricItem$1, {
        isTime: true,
        helpText: helpTexts["averageSubmissionTime"],
        metric: t("neetoMolecules.insights.metrics.avgTimeToComplete"),
        timeSuffix: t("neetoMolecules.insights.mins"),
        value: averageSubmissionTime
      })]
    }), /*#__PURE__*/jsxRuntime.jsxs("div", {
      className: "grid grid-cols-1 gap-6 sm:grid-cols-2 lg:grid-cols-3",
      children: [/*#__PURE__*/jsxRuntime.jsx(MetaInfo$1, {
        data: devices,
        defaultItems: DEFAULT_DEVICES,
        type: t("neetoMolecules.insights.devices")
      }), /*#__PURE__*/jsxRuntime.jsx(MetaInfo$1, {
        data: operatingSystems,
        defaultItems: DEFAULT_OS,
        type: t("neetoMolecules.insights.os")
      }), /*#__PURE__*/jsxRuntime.jsx(MetaInfo$1, {
        data: browsers,
        defaultItems: DEFAULT_BROWSERS,
        type: t("neetoMolecules.insights.browsers")
      })]
    })]
  });
};

module.exports = Insights;
//# sourceMappingURL=Insights.js.map
