'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; }; }(); /*global document, window, alert, slugs, CONFIG */

var _limax = require('limax');

var _limax2 = _interopRequireDefault(_limax);

var _nanoajax = require('nanoajax');

var _nanoajax2 = _interopRequireDefault(_nanoajax);

var _qs = require('qs');

var _qs2 = _interopRequireDefault(_qs);

var _FolderSelect = require('./FolderSelect');

var _FolderSelect2 = _interopRequireDefault(_FolderSelect);

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

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

var FormCreate = function () {
  function FormCreate(parentForm) {
    _classCallCheck(this, FormCreate);

    this._form = parentForm;
    if (typeof this._form !== 'undefined' && this._form !== null) {
      this._isSaving = false;

      // constantes variables
      this._ajax = _nanoajax2.default.ajax;

      // constantes variables DOM elements
      this._previewPostPath = this._form.querySelector('[data-post-path-preview]');

      this._formInputs = [].slice.call(this._form.querySelectorAll('input, select'));
      this._precontribTemplate = [].slice.call(this._form.querySelectorAll('[data-precontrib-templates]'));

      this._selectTemplate = this._form.querySelector('[data-id="selectTemplate"]');
      this._showHideSelect(this._selectTemplate);
      this._handleBtnSelectTemplate = this._btnSelectTemplate.bind(this);

      // // manager update btn
      this._btnCreate = this._form.querySelector('[date-abe-create]');
      this._btnUpdate = this._form.querySelector('[date-abe-update]');
      this._btnDuplicate = this._form.querySelector('[date-abe-duplicate]');
      this._handleBtnDuplicateManagerClick = this._btnDuplicateManagerClick.bind(this);
      this._handleBtnUpdateManagerClick = this._btnUpdateManagerClick.bind(this);
      this._handleBtnCreateManagerClick = this._btnCreateManagerClick.bind(this);
      this._handleBlurEvent = this._blurEvent.bind(this);

      // // init modules
      new _FolderSelect2.default(this._form);

      this._bindEvents();

      this._setSlug(false);
    }
  }

  _createClass(FormCreate, [{
    key: '_bindEvents',
    value: function _bindEvents() {
      if (typeof this._btnUpdate !== 'undefined' && this._btnUpdate !== null) {
        this._btnUpdate.addEventListener('click', this._handleBtnUpdateManagerClick); // click update metadata
      }
      if (typeof this._btnCreate !== 'undefined' && this._btnCreate !== null) {
        this._btnCreate.addEventListener('click', this._handleBtnCreateManagerClick); // click update metadata
      }
      if (typeof this._btnDuplicate !== 'undefined' && this._btnDuplicate !== null) {
        this._btnDuplicate.addEventListener('click', this._handleBtnDuplicateManagerClick); // click duplicate content
      }
      if (typeof this._form !== 'undefined' && this._form !== null) {
        this._form.addEventListener('submit', this._handleSubmit);
      }
      if (typeof this._selectTemplate !== 'undefined' && this._selectTemplate !== null) {
        this._selectTemplate.addEventListener('change', this._handleBtnSelectTemplate);
      }

      Array.prototype.forEach.call(this._formInputs, function (input) {
        input.addEventListener('blur', this._handleBlurEvent);
      }.bind(this));
    }
  }, {
    key: '_blurEvent',
    value: function _blurEvent() {
      this._setSlug(false);
    }
  }, {
    key: '_showHideSelect',
    value: function _showHideSelect(target) {
      this._selectedTemplate = target.value;
      Array.prototype.forEach.call(this._precontribTemplate, function (input) {
        var linkedTpl = input.getAttribute('data-precontrib-templates').split(',');
        var found = false;
        Array.prototype.forEach.call(linkedTpl, function (tpl) {
          if (tpl === this._selectedTemplate) {
            found = true;
          }
        }.bind(this));

        if (found) {
          input.style.display = 'block';
        } else {
          input.style.display = 'none';
        }
      }.bind(this));
    }
  }, {
    key: '_btnSelectTemplate',
    value: function _btnSelectTemplate(e) {
      this._showHideSelect(e.currentTarget);
    }
  }, {
    key: '_setSlug',
    value: function _setSlug(showErrors) {
      var values = {};
      var postPath = '';
      var isValid = true;
      if (this._selectedTemplate != null && this._selectedTemplate != '') {

        Array.prototype.forEach.call(this._formInputs, function (input) {
          if (input.getAttribute('data-slug-type') == 'path') {
            if (input.parentNode.classList.contains('hidden')) {
              return;
            }
          }

          var parentNode = input.parentNode;
          if (parentNode.getAttribute('data-precontrib-templates') == null) {
            parentNode = input.parentNode.parentNode;
          }
          parentNode.classList.remove('has-error');
          var linkedTpl = parentNode.getAttribute('data-precontrib-templates');
          input.parentNode.classList.remove('error');
          if (linkedTpl == null || linkedTpl == this._selectedTemplate) {
            var id = input.getAttribute('data-id');
            var autocomplete = input.getAttribute('data-autocomplete') == 'true' ? true : false;
            var required = input.getAttribute('data-required') == 'true' ? true : false;
            var value = input.value;

            if (autocomplete) {
              var results = input.parentNode.querySelectorAll('.autocomplete-result');
              values[id] = [];
              Array.prototype.forEach.call(results, function (result) {
                var resultValue = result.getAttribute('value');
                if (resultValue.indexOf('{') > -1) {
                  try {
                    var jsonValue = JSON.parse(resultValue);
                    values[id].push(jsonValue);
                  } catch (e) {
                    // values[id].push(value)
                  }
                }
              }.bind(this));
              if (required && values[id].length == 0) {
                isValid = false;
                if (showErrors) parentNode.classList.add('has-error');
              }
            } else {
              if (value.indexOf('{') > -1) {
                try {
                  var jsonValue = JSON.parse(value);
                  values[id] = [jsonValue];

                  if (required && values[id].length == 0) {
                    isValid = false;
                    if (showErrors) parentNode.classList.add('has-error');
                  }
                } catch (e) {
                  // values[id].push(value)
                }
              } else {
                values[id] = value;
                if (required && values[id] == '') {
                  isValid = false;
                  if (showErrors) parentNode.classList.add('has-error');
                }
              }
            }
          }
        }.bind(this));

        var slug = slugs[this._selectedTemplate];
        var slugMatches = slug.match(/{{.*?}}/g);
        if (slugMatches !== null) {
          Array.prototype.forEach.call(slugMatches, function (slugMatch) {
            var cleanSlugMath = slugMatch.replace('{{', '').replace('}}', '');
            try {
              var valueSlug = eval('values.' + cleanSlugMath) + '';
              valueSlug = (0, _limax2.default)(valueSlug, { separateNumbers: false });
              slug = slug.replace(slugMatch, valueSlug);
            } catch (e) {
              slug = slug.replace(slugMatch, '');
              isValid = false;
              // console.error('error on create', e.stack)
            }
          }.bind(this));
        }

        var slugPaths = this._form.querySelectorAll('[data-slug-type=path]');
        Array.prototype.forEach.call(slugPaths, function (slugPath) {
          var isStructureFolder = slugPath.parentNode.getAttribute('data-shown') != null;
          if (slugPath.value != null && slugPath.value != '' && isStructureFolder && !slugPath.parentNode.classList.contains('hidden')) {
            postPath += slugPath.value + '/';
          }
        });
        postPath += slug.replace(/^\//, '');
      } else {
        isValid = false;
      }

      var breadcrumbs = postPath.split('/');
      var breadcrumbsHtml = '';
      Array.prototype.forEach.call(breadcrumbs, function (breadcrumb) {
        var breadcrumbNames = breadcrumb.split('-');
        breadcrumbsHtml += '<li>';
        Array.prototype.forEach.call(breadcrumbNames, function (breadcrumbName) {
          if (breadcrumbName == '' && showErrors) {
            breadcrumbsHtml += '<span class="btn-danger">...</span>-';
          } else {
            breadcrumbsHtml += '<span>' + breadcrumbName + '</span>-';
          }
        }.bind(this));
        breadcrumbsHtml = breadcrumbsHtml.replace(/-$/, '');
        breadcrumbsHtml += '</li>';
      });
      breadcrumbsHtml += '<span>.' + CONFIG.EXTENSION + '</span>';
      this._previewPostPath.innerHTML = '<span>URL : </span>' + breadcrumbsHtml;

      return {
        isValid: isValid,
        postPath: postPath,
        values: values
      };
    }
  }, {
    key: '_submit',
    value: function _submit(type) {
      var _this = this;

      var res = this._setSlug(true);
      var toSave = _qs2.default.stringify(res.values);

      if (res.isValid && !this._isSaving) {
        this._isSaving = true;
        this._ajax({
          url: document.location.origin + '/abe/' + type + '/' + res.postPath,
          body: toSave,
          headers: {},
          method: 'post'
        }, function (code, responseText) {
          _this._isSaving = false;
          var jsonRes = JSON.parse(responseText);
          if (jsonRes.success == 1 && jsonRes.json != null && jsonRes.json.abe_meta != null) {
            window.location.href = window.location.origin + '/abe/editor' + jsonRes.json.abe_meta.link;
          } else {
            console.log(responseText);
            alert('error');
          }
        });
      }
    }
  }, {
    key: '_btnDuplicateManagerClick',
    value: function _btnDuplicateManagerClick(e) {
      e.preventDefault();
      this._submit('duplicate');
    }
  }, {
    key: '_btnUpdateManagerClick',
    value: function _btnUpdateManagerClick(e) {
      e.preventDefault();
      this._submit('update');
    }
  }, {
    key: '_btnCreateManagerClick',
    value: function _btnCreateManagerClick(e) {
      e.preventDefault();
      this._submit('create');
    }
  }]);

  return FormCreate;
}();

exports.default = FormCreate;