"use strict";

var _interopRequireWildcard = require("@babel/runtime-corejs3/helpers/interopRequireWildcard");

var _interopRequireDefault = require("@babel/runtime-corejs3/helpers/interopRequireDefault");

var _Object$defineProperty = require("@babel/runtime-corejs3/core-js-stable/object/define-property");

require("core-js/modules/es.array.join");

_Object$defineProperty(exports, "__esModule", {
  value: true
});

exports.default = void 0;

var _stringify = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/json/stringify"));

var _find = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/find"));

var _extends2 = _interopRequireDefault(require("@babel/runtime-corejs3/helpers/extends"));

var _assign = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/object/assign"));

var _setTimeout2 = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/set-timeout"));

var _isArray = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/array/is-array"));

var _keys = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/object/keys"));

var _map = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/map"));

var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime-corejs3/helpers/slicedToArray"));

var _filter = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/filter"));

var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime-corejs3/helpers/objectWithoutProperties"));

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

var _styles = require("@material-ui/core/styles");

var _ReportViewContent = _interopRequireDefault(require("./ReportViewContent"));

var _ViewWrapper = _interopRequireDefault(require("../ViewWrapper"));

var _Toolbar = _interopRequireDefault(require("../internal/Toolbar"));

var _PrintButton = _interopRequireDefault(require("./PrintButton"));

var _HideFieldsButton = _interopRequireDefault(require("./HideFieldsButton"));

var _strings = require("../utils/strings");

var _stats = _interopRequireDefault(require("../stats"));

// @flow
var hiddenTags = {
  categoryId: true,
  notes: true,
  note: true
};

var ReportView = function ReportView(_ref) {
  var observations = _ref.observations,
      onUpdateObservation = _ref.onUpdateObservation,
      onDeleteObservation = _ref.onDeleteObservation,
      presets = _ref.presets,
      filter = (0, _filter.default)(_ref),
      getMediaUrl = _ref.getMediaUrl,
      otherProps = (0, _objectWithoutProperties2.default)(_ref, ["observations", "onUpdateObservation", "onDeleteObservation", "presets", "filter", "getMediaUrl"]);
  var stats = (0, _react.useMemo)(function () {
    return (0, _stats.default)(observations || []);
  }, [observations]);
  var cx = useStyles();

  var _useState = (0, _react.useState)('a4'),
      _useState2 = (0, _slicedToArray2.default)(_useState, 2),
      paperSize = _useState2[0],
      setPaperSize = _useState2[1];

  var _useState3 = (0, _react.useState)(false),
      _useState4 = (0, _slicedToArray2.default)(_useState3, 2),
      print = _useState4[0],
      setPrint = _useState4[1];

  var _useState5 = (0, _react.useState)(function () {
    var _context, _context2;

    // Lazy initial state to avoid this being calculated on every render
    return (0, _map.default)(_context = (0, _filter.default)(_context2 = (0, _keys.default)(stats)).call(_context2, function (key) {
      // Hacky: don't include categoryId and notes in options of fields you can hide
      var fieldKey = JSON.parse(key);
      var fieldKeyString = (0, _isArray.default)(fieldKey) ? fieldKey[0] : fieldKey;
      if (hiddenTags[fieldKeyString]) return false;
      return true;
    })).call(_context, function (key) {
      var fieldKey = JSON.parse(key);
      var label = (0, _strings.fieldKeyToLabel)(fieldKey);
      return {
        id: key,
        hidden: false,
        label: (0, _isArray.default)(label) ? label.join('.') : label
      };
    });
  }),
      _useState6 = (0, _slicedToArray2.default)(_useState5, 2),
      fieldState = _useState6[0],
      setFieldState = _useState6[1];

  (0, _react.useLayoutEffect)(function () {
    if (!print) return;
    var didCancel = false; // Wait for map to render
    // TODO: SUPER hacky - we need to wait for the map to render

    var timeoutId = (0, _setTimeout2.default)(function () {
      if (didCancel) return;
      window.print();
      setPrint(false);
    }, 3000);
    return function () {
      didCancel = true;
      if (timeoutId) clearTimeout(timeoutId);
    };
  }, [print]);
  return /*#__PURE__*/_react.default.createElement(_ViewWrapper.default, {
    observations: observations,
    onUpdateObservation: onUpdateObservation,
    onDeleteObservation: onDeleteObservation,
    presets: presets,
    filter: filter,
    getMediaUrl: getMediaUrl
  }, function (_ref2) {
    var onClickObservation = _ref2.onClickObservation,
        filteredObservations = _ref2.filteredObservations,
        getPreset = _ref2.getPreset,
        getMedia = _ref2.getMedia;

    // Get preset with fields filtered out
    var getPresetWithFilteredFields = function getPresetWithFilteredFields(observation
    /*: Observation*/
    )
    /*: PresetWithAdditionalFields*/
    {
      var _context3, _context4;

      var preset = getPreset(observation);
      return (0, _assign.default)({}, preset, {
        fields: (0, _filter.default)(_context3 = preset.fields).call(_context3, hiddenFieldsFilter(fieldState)),
        additionalFields: (0, _filter.default)(_context4 = preset.additionalFields).call(_context4, hiddenFieldsFilter(fieldState))
      });
    };

    return /*#__PURE__*/_react.default.createElement("div", {
      className: cx.root
    }, /*#__PURE__*/_react.default.createElement(_Toolbar.default, null, /*#__PURE__*/_react.default.createElement(_PrintButton.default, {
      requestPrint: function requestPrint() {
        return setPrint(true);
      },
      changePaperSize: function changePaperSize(newSize) {
        return setPaperSize(newSize);
      },
      paperSize: paperSize
    }), /*#__PURE__*/_react.default.createElement(_HideFieldsButton.default, {
      fieldState: fieldState,
      onFieldStateUpdate: setFieldState
    })), /*#__PURE__*/_react.default.createElement(_ReportViewContent.default, (0, _extends2.default)({
      onClick: onClickObservation,
      observations: filteredObservations,
      getPreset: getPresetWithFilteredFields,
      getMedia: getMedia,
      paperSize: paperSize,
      print: print
    }, otherProps)));
  });
};

var _default = ReportView;
exports.default = _default;

function hiddenFieldsFilter(fieldState
/*: FieldState*/
) {
  return function (field
  /*: Field*/
  )
  /*: boolean*/
  {
    var state = (0, _find.default)(fieldState).call(fieldState, function (fs) {
      var id = (0, _stringify.default)((0, _isArray.default)(field.key) ? field.key : [field.key]);
      return fs.id === id;
    });
    return state ? !state.hidden : true;
  };
}

var useStyles = (0, _styles.makeStyles)(function (theme) {
  return {
    root: {
      position: 'absolute',
      width: '100%',
      top: 0,
      bottom: 0,
      display: 'flex',
      flexDirection: 'column',
      '@media only print': {
        width: 'auto',
        height: 'auto',
        position: 'static',
        backgroundColor: 'inherit',
        display: 'block'
      }
    }
  };
});
//# sourceMappingURL=ReportView.js.map