"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.displayGeoJSONViewUI = displayGeoJSONViewUI;
exports.extractPropertyDocumentation = extractPropertyDocumentation;
exports.getBBoxUIDisplay = getBBoxUIDisplay;
exports.getChoiceDocumentUIDisplay = getChoiceDocumentUIDisplay;
exports.getChoiceSubDocumentUIDisplay = getChoiceSubDocumentUIDisplay;
exports.getDocumentUIDisplay = getDocumentUIDisplay;
exports.getEnumUIDisplay = getEnumUIDisplay;
exports.getJSONUIDisplay = getJSONUIDisplay;
exports.getMultiPolygonUIDisplay = getMultiPolygonUIDisplay;
exports.getOneOfUIDisplay = getOneOfUIDisplay;
exports.getPointUIDisplay = getPointUIDisplay;
exports.getPolygonUIDisplay = getPolygonUIDisplay;
exports.getRDFLangUIDisplay = getRDFLangUIDisplay;
exports.getSubDocumentUIDisplay = getSubDocumentUIDisplay;
exports.getSysUnitUIDisplay = getSysUnitUIDisplay;
exports.getUIDisplay = getUIDisplay;
exports.getlineStringUIDisplay = getlineStringUIDisplay;
var _react = _interopRequireDefault(require("react"));
var CONST = _interopRequireWildcard(require("../constants"));
var util = _interopRequireWildcard(require("../utils"));
var display = _interopRequireWildcard(require("./displayHelper"));
var geoTemplate = _interopRequireWildcard(require("../arrayHelpers/geoJSONTemplates"));
var _uuid = require("uuid");
var _featureCollectionWidget = require("../mapComponents/featureCollectionWidget");
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 _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
// NORMAL DATA TYPES
function getUIDisplay(args, property, dataType) {
  var key = (0, _uuid.v4)();
  function displayWidget(props) {
    // console.log("key", key)
    // we pass ID at this point since we have sepratae IDs for normal dataTypes & for Sets/ List
    var id = props.idSchema["$id"];

    // normal data type input are being called here 
    // function expects input data and id of field into which user event occurs
    function handleChange(data) {
      if (props.onChange) props.onChange(data);
    }
    return display.displayDataTypesWidget(props, args, property, dataType, id, handleChange);
  }
  return {
    "ui:field": displayWidget
  };
}

// retrieves declaration for selected language
function extractPropertyDocumentation(extractedDocumentation, selectedLanguage) {
  if (Array.isArray(extractedDocumentation)) {
    // includes multi lang support
    var filtered = extractedDocumentation.filter(function (arr) {
      return arr[CONST.SELECTED_LANGUAGE] === selectedLanguage;
    });
    return filtered[0]; //throw new Error(`Expected extracted documentation to be an array, but instead got ${extractedDocumentation}`)
  } else {
    // @documentation is also defined as object sometimes
    return extractedDocumentation;
  }
}

// SUBDOCUMENT UI
function getSubDocumentUIDisplay(args, extracted, property, expanded, linked_to) {
  var type = args.type;

  // at this point extracted will have all of the extracted documents from linked_to
  function displaySubDocumentWidget(props) {
    var id = props.idSchema["$id"];
    return display.displaySubDocument(props, args, extracted, property, expanded, id, null, linked_to);
  }
  return {
    "ui:field": displaySubDocumentWidget
  };
}

// DOCUMENT LINKS UI  
function getDocumentUIDisplay(args, extracted, property, linked_to) {
  function displayDocumentLinkWidget(props) {
    return display.displayDocumentLink(props, args, extracted, property, linked_to);
  }
  return {
    "ui:field": displayDocumentLinkWidget
  };
}

// ENUM UI 
function getEnumUIDisplay(args, property) {
  function displayEnumWidget(props) {
    var id = props.idSchema["$id"],
      hideFieldLabel = false;
    return display.displayEnum(args, props, property, id, hideFieldLabel);
  }
  return {
    "ui:field": displayEnumWidget
  };
}

// rdf:Lang
function getRDFLangUIDisplay(args, property) {
  function rdfLanguageWidget(props) {
    var id = props.idSchema["$id"];
    return display.displayRDFLanguageWidget(args, props, property, id);
  }
  return {
    "ui:field": rdfLanguageWidget
  };
}

// sys:Unit
function getSysUnitUIDisplay(args, property) {
  function sysUnitWidget(props) {
    var id = props.idSchema["$id"];
    return display.displaySysUnitWidget(args, props, property, id);
  }
  return {
    "ui:field": sysUnitWidget
  };
}

// SYS:JSON 
function getJSONUIDisplay(args, property) {
  function displayJSONWidget(props) {
    var id = props.idSchema["$id"];
    return display.displayJSON(props, args, property, id);
  }
  return {
    "ui:field": displayJSONWidget
  };
}

// ONE OF PROPERTY
function getOneOfUIDisplay(args, property) {
  // at this point extracted will have all of the extracted documents from linked_to
  function displayOneOfWidget(props) {
    var id = props.idSchema["$id"];
    return display.displayOneOfProperty(props, args, property, id);
  }
  return {
    "ui:field": displayOneOfWidget
  };
}

// Choice Sub documents
function getChoiceSubDocumentUIDisplay(args, property) {
  function displayChoiceSubDocumentWidget(props) {
    var id = props.idSchema["$id"];
    return display.displayChoiceSubDocument(props, args, property, id);
  }
  return {
    "ui:field": displayChoiceSubDocumentWidget
  };
}

// CHOICE DOCUMENTS 
function getChoiceDocumentUIDisplay(args, property) {
  function displayChoiceDocumentWidget(props) {
    var id = props.idSchema["$id"];
    return display.displayChoiceDocument(props, args, property, id);
  }
  return {
    "ui:field": displayChoiceDocumentWidget
  };
}

// POINT GEO JSONs
function getPointUIDisplay(args, property) {
  var mode = args.mode;
  if (mode === CONST.VIEW) {
    var displayGeoJSONView = function displayGeoJSONView(props) {
      return displayGeoJSONViewUI(props, args, property);
    };
    return {
      "ui:field": displayGeoJSONView
    };
  }
  function showPointUI(props) {
    return display.displayPointEditDocument(props, args, property);
  }
  return {
    "ui:options": CONST.UI_HIDDEN_ARRAY_OPTIONS,
    "ui:ArrayFieldTemplate": showPointUI
  };
}

// LINE STRING GEO JSONs
function getlineStringUIDisplay(args, property) {
  var mode = args.mode;
  if (mode === CONST.VIEW) {
    var displayGeoJSONView = function displayGeoJSONView(props) {
      return displayGeoJSONViewUI(props, args, property);
    };
    return {
      "ui:field": displayGeoJSONView
    };
  }
  function showLineStringUI(props) {
    return display.displayLineStringEditDocument(props, args, property);
  }
  return {
    "ui:options": CONST.GEO_FRAMES_ARRAY_OPTIONS,
    "ui:ArrayFieldTemplate": showLineStringUI
  };
}
function displayGeoJSONViewUI(props, args, property, id) {
  var bounds = util.checkIfBoundsAvailable(args.documentFrame, args.formData);

  //let field = documentFrame[property]
  var documentation = util.checkIfPropertyHasDocumentation(args.extractedDocumentation, property, args.fullFrame[CONST.SELECTED_LANGUAGE]);
  var config = {
    name: property,
    formData: args.formData,
    mode: args.mode,
    label: documentation.label,
    comment: documentation.comment ? documentation.comment : null,
    id: id ? id : props.idSchema["$id"],
    className: "tdb__doc__input",
    //required: props.required,
    bounds: bounds
    //featureData: featureData
  };

  return /*#__PURE__*/_react["default"].createElement(_featureCollectionWidget.TDBFeatureCollectionDocuments, {
    config: config
  });
}

// POLYGON 
function getPolygonUIDisplay(args, property) {
  var mode = args.mode;
  if (mode === CONST.VIEW) {
    var displayGeoJSONView = function displayGeoJSONView(props) {
      return displayGeoJSONViewUI(props, args, property);
    };
    return {
      "ui:field": displayGeoJSONView
    };
  }
  function coordinatesArrayTemplate(props) {
    return display.displayPolygonEditDocument(props, args, property);
  }
  return {
    "ui:ArrayFieldTemplate": geoTemplate.PolygonArrayFieldTemplate,
    "items": {
      "ui:ArrayFieldTemplate": coordinatesArrayTemplate,
      "items": {
        "ui:options": CONST.UI_HIDDEN_ARRAY_OPTIONS
      }
    }
  };
}

// MULTIPOLYGON 
function getMultiPolygonUIDisplay(args, property) {
  var mode = args.mode;
  if (mode === CONST.VIEW) {
    var displayGeoJSONView = function displayGeoJSONView(props) {
      return displayGeoJSONViewUI(props, args, property);
    };
    return {
      "ui:field": displayGeoJSONView
    };
  }
  function coordinatesArrayTemplate(props) {
    return display.displayMultiPolygonEditDocument(args, props, property);
  }
  return {
    "ui:ArrayFieldTemplate": geoTemplate.MultiPolygonArrayFieldTemplate,
    "items": {
      "ui:ArrayFieldTemplate": coordinatesArrayTemplate,
      "items": {
        "ui:options": CONST.UI_HIDDEN_ARRAY_OPTIONS
      }
    }
  };
}

// BINDING BOX 
function getBBoxUIDisplay(args, property) {
  var mode = args.mode;
  if (mode === CONST.VIEW) {
    var displayBBoxUI = function displayBBoxUI(props) {
      var id = props.idSchema["$id"];
      if (props.formData && props.formData.includes(undefined)) return /*#__PURE__*/_react["default"].createElement("div", {
        className: "tdb__".concat(props.name, "__hidden")
      });else return display.displayBBoxDocument(props, args, property, id);
    };
    return {
      "ui:field": displayBBoxUI
    };
  }
  function showBBoxUI(props) {
    return display.displayBBoxEditDocument(args, props, property);
    /*let argsHolder = {...args}
    argsHolder.documentFrame={ [property]: args.documentFrame[property][CONST.CLASS] }
    return geoTemplate.BBoxFieldTemplate(argsHolder, props, property)*/
  }

  return {
    "ui:options": CONST.UI_HIDDEN_ARRAY_OPTIONS,
    "ui:ArrayFieldTemplate": showBBoxUI
  };
}