'use strict';

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

var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };

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, json, alert */

var _iframe = require('../utils/iframe');

var _EditorUtils = require('./EditorUtils');

var _EditorUtils2 = _interopRequireDefault(_EditorUtils);

var _EditorJson = require('../modules/EditorJson');

var _EditorJson2 = _interopRequireDefault(_EditorJson);

var _on = require('on');

var _on2 = _interopRequireDefault(_on);

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 EditorSave = function () {
  function EditorSave() {
    var _this = this;

    _classCallCheck(this, EditorSave);

    this._json = _EditorJson2.default.instance;
    this._saveType = 'draft';

    this.onFileSaved = (0, _on2.default)(this);

    this._abeForm = document.querySelector('#abeForm');
    this._abeDisplayStatus = document.querySelector('[data-display-status]');
    this._abeFormSubmit = document.querySelector('#abeForm button[type=submit]');

    this._handleSubmitClick = this._submitClick.bind(this);

    this._btnSaves = document.querySelectorAll('.btn-save');
    Array.prototype.forEach.call(this._btnSaves, function (btnSave) {
      btnSave.addEventListener('click', _this._handleSubmitClick);
    });

    var pageTpl = document.querySelector('#page-template');
    if (typeof pageTpl !== 'undefined' && pageTpl !== null) {
      document.querySelector('#page-template').addEventListener('load', function () {
        _EditorUtils2.default.checkAttribute();
      });
    }

    if (typeof this._abeForm !== 'undefined' && this._abeForm !== null) {
      this._formSubmit();
    }
  }

  /**
   * return abe form to json
   * @return {Object} json
   */


  _createClass(EditorSave, [{
    key: 'serializeForm',
    value: function serializeForm() {
      var _this2 = this;

      var abeForm = document.getElementById('abeForm');
      if (abeForm == null) return;

      var inputs = [].slice.call(abeForm.querySelectorAll('input'));
      var selects = [].slice.call(abeForm.querySelectorAll('select'));
      inputs = inputs.concat(selects);
      var textareas = [].slice.call(abeForm.querySelectorAll('textarea'));
      inputs = inputs.concat(textareas);

      this._json.data = json;

      Array.prototype.forEach.call(inputs, function (input) {
        var dataId = input.getAttribute('data-id');
        if (input.type === 'file') return;
        if (typeof dataId !== 'undefined' && dataId !== null) {
          if (dataId.indexOf('[') > -1) {
            var obj = dataId.split('[')[0];
            var index = dataId.match(/[^\[]+?(?=\])/)[0];
            var key = dataId.replace(/[^\.]+?-/, '');
            if (typeof _this2._json.data[obj] === 'undefined' || _this2._json.data[obj] === null) _this2._json.data[obj] = [];
            if (typeof _this2._json.data[obj][index] === 'undefined' || _this2._json.data[obj][index] === null) _this2._json.data[obj][index] = {};
            _this2._json.data[obj][index][key] = input.value;
            var emptyObject = 0;
            for (var prop in _this2._json.data[obj][index]) {
              if (_this2._json.data[obj][index][prop].trim() !== '') emptyObject++;
            }
            if (emptyObject === 0) {
              delete _this2._json.data[obj][index];
            }
          } else {
            var value;

            if (input.nodeName === 'SELECT') {
              var checked = input.querySelectorAll('option:checked');
              value = [];
              Array.prototype.forEach.call(checked, function (check) {
                if (check.value !== '') {
                  if (check.value.indexOf('{') > -1 || check.value.indexOf('[') > -1) {
                    value.push(JSON.parse(check.value));
                  } else {
                    value.push(check.value);
                  }
                }
              });
            } else if (input.getAttribute('data-autocomplete') === 'true') {
              var results = input.parentNode.querySelectorAll('.autocomplete-result-wrapper .autocomplete-result');
              value = [];
              Array.prototype.forEach.call(results, function (result) {
                var val = result.getAttribute('value');
                if (val !== '') {
                  if (val.indexOf('{') > -1 || val.indexOf('[') > -1) {
                    value.push(JSON.parse(val));
                  } else {
                    value.push(val);
                  }
                }
              });
            } else {
              value = input.value.replace(/\"/g, '\&quot;') + '';
            }
            _this2._json.data[dataId] = value;
          }
        }
      });
    }
  }, {
    key: 'savePage',
    value: function savePage(type) {
      var _this3 = this;

      var target = document.querySelector('[data-action="' + type + '"]');
      this.serializeForm();
      target.classList.add('loading');
      target.setAttribute('disabled', 'disabled');
      var url = target.getAttribute('data-url');

      this._json.save(this._saveType, url).then(function (result) {
        target.classList.add('done');
        // this._populateFromJson(this._json.data)

        target.classList.remove('loading');
        target.classList.remove('done');
        target.removeAttribute('disabled');

        if (result.success === 1) {
          _this3._abeDisplayStatus.innerHTML = result.json.abe_meta.status;
          window.json = result.json;
        }
        var formWrapper = document.querySelector('#abeForm');
        Array.prototype.forEach.call(formWrapper.classList, function (classStr) {
          if (classStr.indexOf('status-') > -1) formWrapper.classList.remove(classStr);
        });
        formWrapper.classList.add('status-' + result.json.abe_meta.status);
        _this3.onFileSaved._fire();
      }).catch(function (e) {
        console.error(e);
      });
    }

    /**
     * Listen form submit and save page template 
     * @return {void}
     */

  }, {
    key: '_formSubmit',
    value: function _formSubmit() {
      var _this4 = this;

      this._abeForm.addEventListener('submit', function (e) {
        e.preventDefault();
        _this4.savePage(_this4._saveType);
      });
    }
  }, {
    key: '_submitClick',
    value: function _submitClick(e) {
      this._saveType = e.currentTarget.getAttribute('data-action');
      if (this._saveType !== 'draft' && this._saveType !== 'reject') {
        this._abeFormRequired();
      } else {
        this.savePage(this._saveType);
        // this._abeFormSubmit.click()
      }
    }
  }, {
    key: '_abeFormRequired',
    value: function _abeFormRequired() {
      var formGroups = [].slice.call(document.getElementById('abeForm').querySelectorAll('.form-group'));
      var valid = true;

      Array.prototype.forEach.call(formGroups, function (formGroup) {
        var input = formGroup.querySelector('[data-required=true]');
        if (typeof input !== 'undefined' && input !== null) {
          var required = input.getAttribute('data-required');

          var precontrib = formGroup.getAttribute('data-precontrib-templates');

          if (precontrib != null && precontrib != '') {
            if (precontrib != 'json.abe_meta.template') {
              return;
            }
          }

          var autocomplete = input.getAttribute('data-autocomplete');
          if (typeof autocomplete !== 'undefined' && autocomplete !== null && (autocomplete === 'true' || autocomplete === true)) {
            var countValue = input.parentNode.querySelectorAll('.autocomplete-result');
            if (countValue.length <= 0) {
              formGroup.classList.add('has-error');
              valid = false;
            } else {
              formGroup.classList.remove('has-error');
            }
          } else if (typeof required !== 'undefined' && required !== null && (required === 'true' || required === true)) {
            if (input.value === '') {
              formGroup.classList.add('has-error');
              valid = false;
            } else {
              formGroup.classList.remove('has-error');
            }
          }
        }
      });

      if (valid) {
        this.savePage(this._saveType);
        // this._abeFormSubmit.click()
      } else {
        alert('Required fields are missing');
      }
    }

    /**
     * populate all form and iframe html with json
     * @param  {Object} json object with all values
     * @return {null}
     */

  }, {
    key: '_populateFromJson',
    value: function _populateFromJson(json) {
      this._json.data = json;
      var forms = document.querySelectorAll('.form-abe');
      Array.prototype.forEach.call(forms, function (form) {
        var id = form.getAttribute('data-id');
        if (typeof id != 'undefined' && id !== null && typeof json[id] != 'undefined' && json[id] !== null) {
          var value = json[id];
          if ((typeof value === 'undefined' ? 'undefined' : _typeof(value)) === 'object' && Object.prototype.toString.call(value) === '[object Array]') {
            value = JSON.stringify(value);
          } else if ((typeof value === 'undefined' ? 'undefined' : _typeof(value)) === 'object' && Object.prototype.toString.call(value) === '[object Object]') {
            value = JSON.stringify(value);
          }
          form.value = value;

          var node = (0, _iframe.IframeNode)('#page-template', '[data-abe-' + id.replace(/\[([0-9]*)\]/g, '$1') + ']')[0];
          _EditorUtils2.default.formToHtml(node, form);
        }
      });
    }
  }]);

  return EditorSave;
}();

exports.default = EditorSave;