'use strict';

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

var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

var _ = require('../../');

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

var fullAttr = '-abe-(.+?)(?=\.';
var captureAttr = '-abe-(.+?)(?=\.';

/**
 * Class Attr
 * Work string to manage string attributes key/value
 */

var Attr = function () {

  /**
   * @param  {String} str string to work with
   * @return {void}
   */
  function Attr(str) {
    _classCallCheck(this, Attr);

    this.str = str;
    this.val = {};
    this.extract();
  }

  /**
   * @return {Object} attributes extracted from string as an object
   */


  _createClass(Attr, [{
    key: 'extract',
    value: function extract() {
      var rex = new RegExp(captureAttr + this.getExtension() + ')');
      if (rex.test(this.str)) {
        var arrAttr = this.str.match(rex)[0].replace('-abe-', '');
        this.val = { 's': arrAttr[0], 'd': _.cmsData.revision.getStatusAndDateToFileName(arrAttr.slice(1)) };
      }
      return this.val;
    }

    /**
     * @return {String} str without attributes
     */

  }, {
    key: 'remove',
    value: function remove() {
      if (this.str != null) {
        return this.str.replace(new RegExp(fullAttr + this.getExtension() + ')'), '');
      }
      return this.str;
    }
  }, {
    key: 'getExtension',
    value: function getExtension() {
      if (this.str != null) {
        var ext = this.str.split('.');
        return ext[ext.length - 1];
      }
      return '';
    }

    /**
     * Insert attributes to the string
     * @param  {String} string composed of a status (ex: v for validate, d for draft ...) and a date
     * @return {String} the new string with added attributes
     */

  }, {
    key: 'insert',
    value: function insert(newValues) {
      var strWithoutAttr = this.remove();
      strWithoutAttr = strWithoutAttr.replace(new RegExp('\\.' + this.getExtension()), '');
      return strWithoutAttr + '-abe-' + newValues + '.' + this.getExtension();
    }
  }]);

  return Attr;
}();

exports.default = Attr;