"use strict";

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

var _react = require("react");

var _react2 = _interopRequireDefault(_react);

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

function actionFactory(action, actionConfiguration, schema) {
  if (action === "update") {
    return function (cell, row, enumObject, rowIndex, formData, onChange) {
      var activeRecords = [];
      var newFormData = formData.slice(0);
      if (rowIndex != undefined) {
        var filterStatus = actionConfiguration.filterField ? actionConfiguration.filterField : false;
        if (filterStatus) {
          //If filter enabled on the table
          newFormData.map(function (value, index) {
            if (value[actionConfiguration.filterField] === schema[actionConfiguration.filterField]["default"]) {
              value["form_index"] = index;
              activeRecords.push(value);
            }
          });
        } else {
          activeRecords = newFormData;
        }
        activeRecords.map(function (value, index) {
          var form_index = index;
          if (value["form_index"] !== undefined) {
            form_index = value["form_index"];
            delete value["form_index"];
          }
          if (rowIndex === index) {
            if (actionConfiguration.fieldToUpdate !== undefined) {
              var update = [];
              actionConfiguration.fieldToUpdate.map(function (fieldToUpdate) {
                if (schema[fieldToUpdate] !== undefined) {
                  if (schema[fieldToUpdate]["type"] === "boolean") {
                    update[fieldToUpdate] = !value[fieldToUpdate];
                  } // can add separate block for each type of input
                  newFormData[form_index] = Object.assign({}, value, update);
                }
              });
            }
          }
        });
      }
      onChange(newFormData);
    };
  } else if (action === "delete") {
    return function (cell, row, enumObject, rowIndex, formData, onChange) {
      var newFormData = formData.slice(0);
      newFormData.splice(rowIndex, 1);
      onChange(newFormData);
    };
  } else if (action === "moveup") {
    return function (cell, row, enumObject, rowIndex, formData, onChange) {
      var newFormData = formData.slice(0);
      var temp = newFormData[rowIndex];
      if (rowIndex >= 1) {
        newFormData[rowIndex] = newFormData[rowIndex - 1];
        newFormData[rowIndex - 1] = temp;
        onChange(newFormData);
      }
    };
  } else if (action === "movedown") {
    return function (cell, row, enumObject, rowIndex, formData, onChange) {
      var newFormData = formData.slice(0);
      var temp = newFormData[rowIndex];
      if (rowIndex <= formData.length - 2) {
        newFormData[rowIndex] = newFormData[rowIndex + 1];
        newFormData[rowIndex + 1] = temp;
        onChange(newFormData);
      }
    };
  } else if (typeof action === "function") {
    return action;
  } else {
    return undefined;
  }
}

function actionColumnFrom(_ref, schema) {
  var action = _ref.action,
      icon = _ref.icon,
      text = _ref.text,
      _ref$actionConfigurat = _ref.actionConfiguration,
      actionConfiguration = _ref$actionConfigurat === undefined ? false : _ref$actionConfigurat;

  var handleClick = actionFactory(action, actionConfiguration, schema);
  if (!handleClick) {
    return {};
  }

  return {
    dataField: icon,
    dataFormat: function dataFormat(cell, row, enumObject, rowIndex, formData, onChange) {
      return _react2.default.createElement(
        "span",
        {
          onClick: function onClick() {
            return handleClick(cell, row, enumObject, rowIndex, formData, onChange);
          } },
        _react2.default.createElement("i", { className: icon }),
        text
      );
    },
    editable: false
  };
}

var actionToCol = function actionToCol(formData, onChange, schema) {
  return function (actionConf) {
    var genericConf = actionColumnFrom(actionConf, schema);
    var realDataFormat = actionConf.dataFormat ? actionConf.dataFormat : genericConf.dataFormat;
    return Object.assign({}, actionConf, genericConf, {
      dataFormat: function dataFormat(cell, row, enumObject, rowIndex) {
        return realDataFormat(cell, row, enumObject, rowIndex, formData, onChange);
      }
    });
  };
};

function actionHeadersFrom(schema, uiSchema, formData, onChange) {
  var _uiSchema$table = uiSchema.table;
  _uiSchema$table = _uiSchema$table === undefined ? {} : _uiSchema$table;
  var _uiSchema$table$right = _uiSchema$table.rightActions,
      rightActions = _uiSchema$table$right === undefined ? [] : _uiSchema$table$right,
      _uiSchema$table$leftA = _uiSchema$table.leftActions,
      leftActions = _uiSchema$table$leftA === undefined ? [] : _uiSchema$table$leftA;
  var _schema$items$propert = schema.items.properties,
      properties = _schema$items$propert === undefined ? [] : _schema$items$propert;


  var rightColumns = rightActions.map(actionToCol(formData, onChange, properties));
  var leftColumns = leftActions.map(actionToCol(formData, onChange, properties));
  return { rightColumns: rightColumns, leftColumns: leftColumns };
}