"use strict";

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); }
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.FrameViewer = FrameViewer;
var _react = _interopRequireWildcard(require("react"));
var _FrameHelpers = require("./FrameHelpers");
var CONST = _interopRequireWildcard(require("./constants"));
var _reactBootstrap = require("react-bootstrap");
var util = _interopRequireWildcard(require("./utils"));
var _Viewer = require("./Viewer");
var _HelperMessages = require("./HelperMessages");
var _constructFormParams = require("./constructFormParams");
var _reactBootswatchSelect = require("react-bootswatch-select");
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
function _iterableToArrayLimit(arr, i) { var _i = null == arr ? null : "undefined" != typeof Symbol && arr[Symbol.iterator] || arr["@@iterator"]; if (null != _i) { var _s, _e, _x, _r, _arr = [], _n = !0, _d = !1; try { if (_x = (_i = _i.call(arr)).next, 0 === i) { if (Object(_i) !== _i) return; _n = !1; } else for (; !(_n = (_s = _x.call(_i)).done) && (_arr.push(_s.value), _arr.length !== i); _n = !0); } catch (err) { _d = !0, _e = err; } finally { try { if (!_n && null != _i["return"] && (_r = _i["return"](), Object(_r) !== _r)) return; } finally { if (_d) throw _e; } } return _arr; } }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
//import { loadTheme } from "./formActions"

/*
**  frame     - The database Class Frame, or object of all class frames
**  uiFrame   - ui json of a document
**  type      - document type of interest
**  mode      - create/ edit/ view
**  submitButton - submit button configuration json object
**  formData  - filled value of the document
**  onSubmit  - a function with have custom logic to process data submitted
**  onSelect   - a js function which gets back the selected value from selects
**  onTraverse - a js function which gets back the ID of a document on click
**  compareFormData - used for diff viewers to compare against original or changed data 
**  language - language code parameters to support a wide variety of languages in Ui as defined in schema
**  showThemeSelector - a Select to select differnet themes when using Themes on an application level 
**  theme - a default theme in which Form will be displayed
*/
function FrameViewer(props) {
  var frame = props.frame,
    uiFrame = props.uiFrame,
    type = props.type,
    mode = props.mode,
    formData = props.formData,
    onSubmit = props.onSubmit,
    showThemeSelector = props.showThemeSelector,
    language = props.language,
    theme = props.theme;

  // schema constants
  var _useState = (0, _react.useState)(false),
    _useState2 = _slicedToArray(_useState, 2),
    schema = _useState2[0],
    setSchema = _useState2[1];
  // ui schema constants
  var _useState3 = (0, _react.useState)(false),
    _useState4 = _slicedToArray(_useState3, 2),
    uiSchema = _useState4[0],
    setUISchema = _useState4[1];
  // display constants which holds schema & ui Schema to refresh Viewer
  var _useState5 = (0, _react.useState)(false),
    _useState6 = _slicedToArray(_useState5, 2),
    display = _useState6[0],
    setDisplay = _useState6[1];
  // read only constants (used in VIEW mode)
  var _useState7 = (0, _react.useState)(false),
    _useState8 = _slicedToArray(_useState7, 2),
    readOnly = _useState8[0],
    setReadOnly = _useState8[1];
  // language support constants
  var _useState9 = (0, _react.useState)(false),
    _useState10 = _slicedToArray(_useState9, 2),
    lang = _useState10[0],
    setLanguage = _useState10[1];
  // error constants
  var _useState11 = (0, _react.useState)(false),
    _useState12 = _slicedToArray(_useState11, 2),
    error = _useState12[0],
    setError = _useState12[1];
  // documentation constants
  var _useState13 = (0, _react.useState)(false),
    _useState14 = _slicedToArray(_useState13, 2),
    documentation = _useState14[0],
    setDocumentation = _useState14[1];
  // form data constants 
  var _useState15 = (0, _react.useState)(formData),
    _useState16 = _slicedToArray(_useState15, 2),
    data = _useState16[0],
    setData = _useState16[1];
  // message constants 
  var _useState17 = (0, _react.useState)(false),
    _useState18 = _slicedToArray(_useState17, 2),
    message = _useState18[0],
    setMessage = _useState18[1];
  // look up constants which maintains a reference to all of the class definitions
  var _useState19 = (0, _react.useState)({}),
    _useState20 = _slicedToArray(_useState19, 2),
    reference = _useState20[0],
    setReference = _useState20[1];
  // constants to update form props 
  var _useState21 = (0, _react.useState)(false),
    _useState22 = _slicedToArray(_useState21, 2),
    update = _useState22[0],
    setUpdate = _useState22[1];
  function clear() {
    // reset everything on change of form props
    setDisplay(false);
    setSchema(false);
    setUISchema(false);
    setReadOnly(false);
    setLanguage(false);
  }

  /*useEffect(() => {
  	if(theme) {
  		loadTheme(theme)
  	}
  }, [theme])*/

  (0, _react.useEffect)(function () {
    try {
      if (frame && type && mode) {
        clear();
        // update form
        setUpdate(Date.now());
      }
    } catch (e) {
      setError("An error has occured in generating frames. Err - ".concat(e));
    }
  }, [frame, uiFrame, type, mode, formData, language]);
  (0, _react.useEffect)(function () {
    // on update construct form params 
    if (update) {
      (0, _constructFormParams.constructFormParams)(props, setDocumentation, reference, setReference, setReadOnly, setMessage, display, setDisplay);
    }
  }, [update]);
  if (error) {
    return /*#__PURE__*/_react["default"].createElement(_reactBootstrap.Alert, {
      variant: "danger"
    }, error);
  }
  return /*#__PURE__*/_react["default"].createElement("div", {
    className: "tdb__frame__viewer "
  }, /*#__PURE__*/_react["default"].createElement(_reactBootswatchSelect.BootswatchSelect, {
    version: '4.4.1',
    selectedThemeName: theme,
    selectorHidden: true
  }), showThemeSelector && /*#__PURE__*/_react["default"].createElement("div", {
    className: "mb-3 d-flex"
  }, /*#__PURE__*/_react["default"].createElement("small", {
    className: "text-muted"
  }, "Theme Selector: "), /*#__PURE__*/_react["default"].createElement(_reactBootswatchSelect.BootswatchSelect, {
    version: '4.4.1',
    selectedThemeName: theme ? theme : "darkly"
  })), /*#__PURE__*/_react["default"].createElement(_HelperMessages.HelperMessages, {
    frame: frame,
    mode: mode,
    type: type,
    formData: formData
  }), /*#__PURE__*/_react["default"].createElement(_Viewer.Viewer, {
    display: display,
    message: message,
    mode: mode,
    type: type,
    language: language,
    onSubmit: onSubmit,
    readOnly: readOnly,
    data: formData,
    setData: setData,
    documentation: documentation
  }));
}