"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.handleSubmit = void 0;
exports.loadTheme = loadTheme;
var _constants = require("./constants");
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); }
function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
// function to remove all empty json object 
// this is to remove all optional subdocument data other wise database will 
// complain on submit
function removeEmptyObject(data) {
  if (_typeof(data) !== 'object') {
    return data;
  }
  return Object.keys(data).reduce(function (accumulator, key) {
    //const isObject = typeof data[key] === 'object';
    var isObject = !Array.isArray(data[key]) && data[key] !== null && _typeof(data[key]) === 'object' ? true : false;
    var value = isObject ? removeEmptyObject(data[key]) : data[key];
    var isEmptyObject = isObject && !Object.keys(value).length;
    if (value === null && value === undefined || isEmptyObject) {
      return accumulator;
    }
    return Object.assign(accumulator, _defineProperty({}, key, value));
  }, {});
}
function removeOneOfPropertyFromDocumentLevel(data) {
  if (!data.hasOwnProperty(_constants.ONEOFVALUES)) return data;
  var newData = data;
  var oneOfData = data[_constants.ONEOFVALUES];
  delete newData[_constants.ONEOFVALUES];
  for (var choice in oneOfData) {
    newData[choice] = oneOfData[choice];
  }
  return newData;
}

/**
* 
* @param {*} data - data extracted from the form 
* @param {*} onSubmit - callback on Submit which will contain the data submitted onClick of Submit button
* @param {*} setData - useState constant to show modified data on form
* @param {*} type - document type of interest
* @param {*} mode - Create | Edit | View
* @returns extracted data to onSubmit callback function 
*/
var handleSubmit = function handleSubmit(data, onSubmit, setData, type, mode) {
  if (onSubmit) {
    var alteredFormData = removeEmptyObject(data.formData);
    var formData = removeOneOfPropertyFromDocumentLevel(alteredFormData);
    console.log("Before submit: ", formData);
    setData(formData);
    var extracted = formData;
    //var extracted = transformData(mode, schema.properties, formData, frame, type)
    // add document type 
    extracted["@type"] = type;
    if (mode === _constants.EDIT &&
    // append id in edit mode
    extracted && !extracted.hasOwnProperty("@id") && formData.hasOwnProperty("@id")) {
      extracted["@id"] = formData["@id"];
    }
    onSubmit(extracted);
    console.log("Data submitted: ", extracted);
    console.log("Data submitted: STRINGIFY", JSON.stringify(extracted, null, 2));
    return extracted;
  }
};
exports.handleSubmit = handleSubmit;
function loadTheme(theme) {
  // dynamically add bootstrap library
  var link = document.createElement('link');
  link.rel = "stylesheet";
  link.type = "text/css";
  link.href = "https://bootswatch.com/5/".concat(theme, "/bootstrap.css");
  document.head.appendChild(link);
}