'use strict';

var asyncToGenerator = require('./asyncToGenerator-65df6bf8.js');
var toConsumableArray = require('./toConsumableArray-d7797c2b.js');
var stringify = require('./stringify-f7a37a8d.js');
var slicedToArray = require('./slicedToArray-067dd3bc.js');
var moment = require('./moment-aca40de1.js');
var React = require('react');
var reactBootstrap = require('react-bootstrap');
require('./_commonjsHelpers-06173234.js');
require('./core.get-iterator-method-5643aa10.js');
require('./web.dom.iterable-d98303e0.js');
require('./_library-dd23b178.js');
require('./_set-species-0c7864a2.js');
require('./_iter-detect-b1df62f0.js');

function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

var React__default = /*#__PURE__*/_interopDefaultLegacy(React);

function PtceDeck(props) {
  var _this = this;

  var initialData = props.initialData;

  var _useState = React.useState(),
      _useState2 = slicedToArray._slicedToArray(_useState, 2),
      cmeData = _useState2[0],
      setCmeData = _useState2[1];

  var _useState3 = React.useState(),
      _useState4 = slicedToArray._slicedToArray(_useState3, 2),
      hasMore = _useState4[0],
      setHasMore = _useState4[1];

  var _useState5 = React.useState(),
      _useState6 = slicedToArray._slicedToArray(_useState5, 2),
      paginatedPoint = _useState6[0],
      setPaginatedPoint = _useState6[1];

  React.useEffect(function () {
    //set inital data
    if (initialData && initialData.pageInfo) {
      setHasMore(initialData.pageInfo.hasMore);
      setPaginatedPoint(initialData.pageInfo.cursor);
      setCmeData(initialData.contentItems);
    }
    //set hasMore and breakpoint
  }, []);

  var loadMore = function () {
    var _ref = asyncToGenerator._asyncToGenerator( /*#__PURE__*/asyncToGenerator.regenerator.mark(function _callee() {
      var response, data;
      return asyncToGenerator.regenerator.wrap(function _callee$(_context) {
        while (1) {
          switch (_context.prev = _context.next) {
            case 0:
              _context.next = 2;
              return fetch('/api/get_activities-CE', {
                headers: {
                  'Content-Type': 'application/json',
                  Accept: 'application/json'
                },
                method: 'post',
                credentials: 'same-origin',
                body: stringify._JSON$stringify({ params: { paginatedPoint: paginatedPoint } })
              });

            case 2:
              response = _context.sent;
              _context.next = 5;
              return response.json();

            case 5:
              data = _context.sent;


              if (data.pageInfo) {
                setHasMore(data.pageInfo.hasMore);
                setPaginatedPoint(data.pageInfo.cursor);
                setCmeData(function (prevState) {
                  return [].concat(toConsumableArray._toConsumableArray(prevState), toConsumableArray._toConsumableArray(data.contentItems));
                });
              }

            case 7:
            case 'end':
              return _context.stop();
          }
        }
      }, _callee, _this);
    }));

    return function loadMore() {
      return _ref.apply(this, arguments);
    };
  }();

  return React__default['default'].createElement(
    'div',
    null,
    cmeData && cmeData.map(function (activity) {
      var href = activity.url ? activity.url : 'https://www.pharmacytimes.org/courses/' + activity.slug;
      return React__default['default'].createElement(
        reactBootstrap.Col,
        { xl: 12, style: { marginBottom: '1rem' } },
        React__default['default'].createElement(
          reactBootstrap.Row,
          null,
          React__default['default'].createElement(
            reactBootstrap.Col,
            { xs: 3, sm: 4, md: 4, lg: 4, xl: 4, style: { padding: '0' } },
            React__default['default'].createElement(
              'a',
              { href: href, rel: 'noopener', target: '_blank' },
              React__default['default'].createElement('img', { style: { width: '100%', borderRadius: '0 0 0 10%', marginBottom: '20px', borderTop: '1px solid #EEE' }, src: activity.asset })
            )
          ),
          React__default['default'].createElement(
            reactBootstrap.Col,
            { xs: 9, sm: 8, md: 8, lg: 8, xl: 8, style: { paddingTop: '10px', marginBottom: '20px', borderTop: '1px solid #EEE' } },
            React__default['default'].createElement(
              'a',
              { href: href, rel: 'noopener', target: '_blank' },
              React__default['default'].createElement('h4', { className: 'mb-1', style: { fontWeight: '500', fontSize: '22px' }, dangerouslySetInnerHTML: { __html: activity.title } }),
              React__default['default'].createElement(
                'p',
                { className: 'mb-2' },
                'Expiration Date: ',
                moment.moment(activity.courseEndDate).format('MMM Do YYYY')
              ),
              React__default['default'].createElement('p', { dangerouslySetInnerHTML: { __html: activity.description } })
            )
          )
        )
      );
    }),
    hasMore && React__default['default'].createElement(
      'div',
      { className: 'text-center my-5' },
      React__default['default'].createElement(
        'button',
        { onClick: loadMore, className: 'btn btn-lg btn-primary' },
        'Load More'
      )
    )
  );
}

module.exports = PtceDeck;
