import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
import _extends from "@babel/runtime/helpers/esm/extends";
import _taggedTemplateLiteral from "@babel/runtime/helpers/esm/taggedTemplateLiteral";

function _templateObject() {
  var data = _taggedTemplateLiteral(["\n\t0% {\n\t\ttransform: translateY(200px) scaleY(2.5) scaleX(.2);\n\t\ttransform-origin: 50% 0%;\n\t\tfilter: blur(40px);\n\t\topacity: 0;\n\t}\n\t100% {\n\t\ttransform: translateY(0) scaleY(1) scaleX(1);\n\t\ttransform-origin: 50% 50%;\n\t\tfilter: blur(0);\n\t\topacity: 1;\n\t}\n}\n"]);

  _templateObject = function _templateObject() {
    return data;
  };

  return data;
}

import React, { forwardRef, memo, useState } from 'react';
import PropTypes from 'prop-types';
import { keyframes } from '@emotion/core';
import { map, o, toPairs } from 'ramda';
import { isBoolean, isNumeric, isString } from 'ramda-extension';
import Box from '../Box';
import Flex from '../Flex';
import ConsoleItem from './ConsoleItem';
import { useConsoleLog } from './hooks';
var jumpInFromBottom = keyframes(_templateObject());
var easing = 'cubic-bezier(.455, .030, .515, .955)';
/* eslint-disable max-len */

var _ref = /*#__PURE__*/React.createElement("polygon", {
  fill: "currentColor",
  points: "10 1.03916875e-13 11.4061556 1.42222591 5.70307778 7.06086213 0 1.42222591 1.40615556 1.03916875e-13 5.70307778 4.24811295"
});

var DropdownArrow = /*#__PURE__*/memo( /*#__PURE__*/forwardRef(function (props, ref) {
  return /*#__PURE__*/React.createElement(Box, _extends({
    ref: ref,
    as: "svg",
    width: "13px",
    height: "9px",
    viewBox: "0 0 13 9",
    xmlns: "http://www.w3.org/2000/svg"
  }, props), _ref);
}));
DropdownArrow.displayName = 'DropdownArrow';
export var defaultFormattingFunction = function defaultFormattingFunction(x) {
  if (isString(x)) {
    return x;
  } // if (isNumeric(x)) {
  // 	return <FormattedNumber value={x} maximumFractionDigits={1} />;
  // }


  if (isNumeric(x)) {
    return Math.round(x * 1000) / 1000;
  }

  if (isBoolean(x)) {
    return x ? 'True' : 'False';
  }

  return '-';
};
/* eslint-enable max-len */

var Console = function Console(_ref2) {
  var title = _ref2.title,
      _ref2$formatValue = _ref2.formatValue,
      formatValue = _ref2$formatValue === void 0 ? defaultFormattingFunction : _ref2$formatValue,
      initiallyOpened = _ref2.initiallyOpened,
      rest = _objectWithoutProperties(_ref2, ["title", "formatValue", "initiallyOpened"]);

  var _useState = useState(initiallyOpened),
      _useState2 = _slicedToArray(_useState, 2),
      isOpened = _useState2[0],
      setIsOpened = _useState2[1];

  var log = useConsoleLog();
  return /*#__PURE__*/React.createElement(Box, _extends({
    sx: {
      position: 'fixed',
      right: '0',
      bottom: 0,
      zIndex: 99999,
      animationName: jumpInFromBottom,
      animationDuration: '.5s',
      animationTimingFunction: easing,
      animationFillMode: 'both',
      animationDelay: '.75s',
      backgroundColor: '#3B3B3B',
      color: 'background',
      maxHeight: '100vh',
      minWidth: ['auto', '400px'],
      width: ['100%', '33%']
    }
  }, rest), /*#__PURE__*/React.createElement(Flex, {
    sx: {
      py: 2,
      px: 3,
      alignItems: 'center',
      cursor: 'pointer'
    },
    onClick: function onClick() {
      return setIsOpened(!isOpened);
    }
  }, /*#__PURE__*/React.createElement(Box, {
    as: "span",
    fontFamily: "mono",
    my: 0,
    sx: {
      pointerEvents: 'none'
    }
  }, title), /*#__PURE__*/React.createElement(DropdownArrow, {
    sx: {
      marginLeft: 'auto',
      display: 'inline',
      transform: "rotate(".concat(isOpened ? '0' : '180', "deg)")
    }
  })), isOpened && /*#__PURE__*/React.createElement(Box, {
    sx: {
      borderTopColor: 'background',
      overflowY: 'auto',
      height: '300px',
      borderTopStyle: 'solid',
      borderTopWidth: '1px'
    }
  }, log && o(map(function (_ref3) {
    var _ref4 = _slicedToArray(_ref3, 2),
        label = _ref4[0],
        value = _ref4[1];

    return /*#__PURE__*/React.createElement(ConsoleItem, {
      key: label,
      label: label,
      value: formatValue(value)
    });
  }), toPairs)(log)));
};

process.env.NODE_ENV !== "production" ? Console.propTypes = {
  children: PropTypes.node,
  formatValue: PropTypes.func,
  initiallyOpened: PropTypes.bool,
  title: PropTypes.node
} : void 0;
export default Console;