"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.inPost = exports.templates = exports.tagTemplate = exports.default = void 0;

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(source, true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(source).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

const tagTemplate = (name, content) => `\\s*\\[${name}\\](${content})\\[\\/${name}\\]\\s*`;

exports.tagTemplate = tagTemplate;
const regExps = [{
  key: 'name',
  pattern: '.*'
}, {
  key: 'allday',
  pattern: 'true|false'
}, {
  key: 'startDate',
  pattern: '[0-9]+'
}, {
  key: 'endDate',
  pattern: '[0-9]+'
}, {
  key: 'reminders',
  pattern: '\\[[0-9, ]*\\]'
}, {
  key: 'location',
  pattern: '.*'
}, {
  key: 'description',
  pattern: '[\\s\\S]*'
}, {
  key: 'mandatory',
  pattern: 'true|false'
}].map(({
  key,
  pattern
}) => ({
  key,
  pattern: tagTemplate(key, pattern)
}));
regExps.push({
  key: 'repeats',
  pattern: '\\s*(?:\\[repeats\\](.*)\\[\\/repeats\\])?\\s*'
});
const inPost = new RegExp('(\\[event(?:\\-invalid)?\\][\\s\\S]+\\[\\/event(?:\\-invalid)?\\])');
exports.inPost = inPost;
const full = regExps.map(r => r.pattern).join('');
const eventRegExp = tagTemplate('event', full);

const parse = text => {
  const matches = text.match(eventRegExp);

  if (!matches || !matches.length) {
    return null;
  }

  const match = matches[1];
  const results = {};
  regExps.forEach(({
    key,
    pattern
  }) => {
    const m = match.match(pattern);
    results[key] = m && m[1] && m[1].trim();
  });
  results.repeats = match.match(/\[repeats\](.*)\[\/repeats\]/);
  results.repeats = results.repeats && results.repeats[1];

  try {
    return {
      name: results.name,
      allday: results.allday === 'true',
      startDate: parseInt(results.startDate, 10),
      endDate: parseInt(results.endDate, 10),
      reminders: JSON.parse(results.reminders),
      location: results.location,
      description: results.description,
      mandatory: results.mandatory === 'true',
      repeats: results.repeats ? JSON.parse(results.repeats.replace(/&quot;/g, '"')) : null
    };
  } catch (e) {
    return null;
  }
};

exports.default = parse;

const templates = _objectSpread({}, regExps, {
  event: eventRegExp
});

exports.templates = templates;
//# sourceMappingURL=parse.js.map