'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _propTypes = require('prop-types');

var _propTypes2 = _interopRequireDefault(_propTypes);

var _reactIntl = require('react-intl');

var _immutable = require('immutable');

var _immutable2 = _interopRequireDefault(_immutable);

var _TitleBar = require('../sections/TitleBar');

var _TitleBar2 = _interopRequireDefault(_TitleBar);

var _SearchPanelContainer = require('../../containers/search/SearchPanelContainer');

var _SearchPanelContainer2 = _interopRequireDefault(_SearchPanelContainer);

var _searchOperators = require('../../constants/searchOperators');

var _DashboardPage = require('../../../styles/cspace-ui/DashboardPage.css');

var _DashboardPage2 = _interopRequireDefault(_DashboardPage);

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

const messages = (0, _reactIntl.defineMessages)({
  title: {
    'id': 'dashboardPage.title',
    'defaultMessage': 'My CollectionSpace'
  },
  recentPanelTitle: {
    'id': 'dashboardPage.recentPanelTitle',
    'defaultMessage': 'Recently Updated Records'
  }
});

const propTypes = {
  history: _propTypes2.default.object
};

const contextTypes = {
  config: _propTypes2.default.object.isRequired
};

const recordType = 'all';

const getSearchDescriptor = () => {
  const range = 7 * 24 * 60 * 60 * 1000; // 7 days
  const afterDate = new Date(Date.now() - range);

  return _immutable2.default.fromJS({
    recordType,
    searchQuery: {
      as: {
        op: _searchOperators.OP_GTE,
        path: 'ns2:collectionspace_core/updatedAt',
        value: afterDate.toISOString()
      },
      size: 20
    }
  });
};

class DashboardPage extends _react.Component {
  constructor() {
    super();

    this.handleSearchDescriptorChange = this.handleSearchDescriptorChange.bind(this);

    this.state = {
      searchDescriptor: getSearchDescriptor()
    };
  }

  handleSearchDescriptorChange(searchDescriptor) {
    this.setState({
      searchDescriptor
    });
  }

  render() {
    const config = this.context.config;
    const history = this.props.history;
    const searchDescriptor = this.state.searchDescriptor;


    const title = _react2.default.createElement(_reactIntl.FormattedMessage, messages.title);
    const recentPanelTitle = _react2.default.createElement(_reactIntl.FormattedMessage, messages.recentPanelTitle);

    return _react2.default.createElement(
      'div',
      { className: _DashboardPage2.default.common },
      _react2.default.createElement(_TitleBar2.default, { title: title, updateDocumentTitle: true }),
      _react2.default.createElement(_SearchPanelContainer2.default, {
        config: config,
        history: history,
        name: 'dashboardRecentPanel',
        searchDescriptor: searchDescriptor,
        title: recentPanelTitle,
        recordType: recordType,
        showSearchButton: false,
        onSearchDescriptorChange: this.handleSearchDescriptorChange
      })
    );
  }
}

exports.default = DashboardPage;
DashboardPage.propTypes = propTypes;
DashboardPage.contextTypes = contextTypes;