"use strict";

exports.__esModule = true;
exports.toJsonCargo = exports.setRequiredBooleansDefault = exports.isTieredAttribute = exports.getPropertyBagFormattedPrimitiveValue = exports.formatNumber = exports.fixPropertyBagModifiedModel = exports.fixPropertyBagEmptyValues = exports.customDataType = exports.createJsonCargo = void 0;
var _constants = require("../constants");
var _propertyValidator = require("./propertyValidator");
var _sharedMessages = _interopRequireDefault(require("../sharedMessages"));
var _parseHelper = require("./parseHelper");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
(function () {
  var enterModule = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.enterModule : undefined;
  enterModule && enterModule(module);
})();
(function () {
  var enterModule = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.enterModule : undefined;
  enterModule && enterModule(module);
})();
var __signature__ = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.default.signature : function (a) {
  return a;
};
var __signature__ = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.default.signature : function (a) {
  return a;
};
var customDataType = exports.customDataType = {
  money: "Money",
  moneyDecimal: "MoneyDecimal",
  priceTieredRateTable: "PriceTieredRateTable",
  quantityTieredRateTable: "QuantityTieredRateTable",
  password: "Password",
  carrierProviderSelector: "CarrierProviderSelector",
  routingProviderSelector: "RoutingProviderSelector",
  multipleCarrierProvidersSelector: "MultipleCarrierProvidersSelector",
  serviceLevelSelector: "ServiceLevelSelector",
  percentageDecimal: "PercentageDecimal",
  carrierReferenceFieldSelector: "CarrierReferenceFieldSelector"
};
var tieredAttributeTypes = [customDataType.priceTieredRateTable, customDataType.quantityTieredRateTable];
var isTieredAttribute = exports.isTieredAttribute = function isTieredAttribute(attribute) {
  return tieredAttributeTypes.includes(attribute.customDataType);
};
var buildValueOfType = function buildValueOfType(cargoType) {
  return "ValueOf".concat(cargoType);
};
var buildValueContainerOfType = function buildValueContainerOfType(dotNetType) {
  return "Orckestra.Overture.Entities.ValueContainer`1[[".concat(dotNetType, ", mscorlib]], Orckestra.Overture.Entities");
};
var createJsonCargo = exports.createJsonCargo = function createJsonCargo(cargoType, value) {
  return {
    __type: buildValueOfType(cargoType),
    value: value
  };
};
var createTieredTableJsonCargo = function createTieredTableJsonCargo(tiers) {
  return {
    __type: "Orckestra.Overture.Entities.Orders.TieredRateTable, Orckestra.Overture.Entities",
    tiers: tiers.filter(function (tier) {
      var _tier$min;
      return ((_tier$min = tier == null ? void 0 : tier.min) != null ? _tier$min : "") !== "";
    })
  };
};
var toJsonCargo = exports.toJsonCargo = function toJsonCargo(attribute, value) {
  switch (attribute.dataType) {
    case _constants.attributeDataType.text:
    case _constants.attributeDataType.lookup:
    case _constants.attributeDataType.singleChoice:
      return value;
    case _constants.attributeDataType.multipleChoice:
      return createJsonCargo(_constants.jsonCargoType.stringArray, value);
    case _constants.attributeDataType.boolean:
      return createJsonCargo(_constants.jsonCargoType.boolean, value);
    case _constants.attributeDataType.integer:
      return createJsonCargo(_constants.jsonCargoType.integer, Number(formatNumber(value, 0)));
    case _constants.attributeDataType.decimal:
      return createJsonCargo(_constants.jsonCargoType.double, Number(value));
    case _constants.attributeDataType.dateTime:
      return createJsonCargo(_constants.jsonCargoType.dateTime, value);
    case _constants.attributeDataType.customType:
      switch (attribute.customDataType) {
        case customDataType.money:
          return createJsonCargo(_constants.jsonCargoType.double, Number(formatNumber(value, 2)));
        case customDataType.moneyDecimal:
        case customDataType.percentageDecimal:
          return createJsonCargo(_constants.jsonCargoType.decimal, Number(formatNumber(value, 2)));
        case customDataType.priceTieredRateTable:
        case customDataType.quantityTieredRateTable:
          return createTieredTableJsonCargo(value);
        case customDataType.password:
        case customDataType.carrierProviderSelector:
        case customDataType.routingProviderSelector:
        case customDataType.multipleCarrierProvidersSelector:
        case customDataType.serviceLevelSelector:
        case customDataType.carrierReferenceFieldSelector:
          return value;
        default:
          throw new Error("toJsonCargo: attribute.customDataType ".concat(attribute.customDataType, " is not implemented"));
      }
    case _constants.attributeDataType.entityReference:
      return createJsonCargo(_constants.jsonCargoType.entityReferences, value);
    default:
      throw new Error("toJsonCargo: attribute.dataType ".concat(attribute.dataType, " is not implemented"));
  }
};
var dataTypesChecker = {
  booleanDataTypes: [buildValueOfType(_constants.propertyBagPrimitiveDataType.boolean), buildValueContainerOfType(_constants.dotNetDataTypes.boolean)],
  integerDataTypes: [buildValueOfType(_constants.propertyBagPrimitiveDataType.byte), buildValueContainerOfType(_constants.dotNetDataTypes.byte), buildValueOfType(_constants.propertyBagPrimitiveDataType.sbyte), buildValueContainerOfType(_constants.dotNetDataTypes.sbyte), buildValueOfType(_constants.propertyBagPrimitiveDataType.int16), buildValueContainerOfType(_constants.dotNetDataTypes.int16), buildValueOfType(_constants.propertyBagPrimitiveDataType.uint16), buildValueContainerOfType(_constants.dotNetDataTypes.uint16), buildValueOfType(_constants.propertyBagPrimitiveDataType.int32), buildValueContainerOfType(_constants.dotNetDataTypes.int32), buildValueOfType(_constants.propertyBagPrimitiveDataType.uint32), buildValueContainerOfType(_constants.dotNetDataTypes.uint32), buildValueOfType(_constants.propertyBagPrimitiveDataType.int64), buildValueContainerOfType(_constants.dotNetDataTypes.int64), buildValueOfType(_constants.propertyBagPrimitiveDataType.uint64), buildValueContainerOfType(_constants.dotNetDataTypes.uint64)],
  textDataTypes: [buildValueOfType(_constants.propertyBagPrimitiveDataType.char), buildValueContainerOfType(_constants.dotNetDataTypes.char), buildValueOfType(_constants.propertyBagPrimitiveDataType.string), buildValueContainerOfType(_constants.dotNetDataTypes.string)],
  decimalDataTypes: [buildValueOfType(_constants.propertyBagPrimitiveDataType.single), buildValueContainerOfType(_constants.dotNetDataTypes.single), buildValueOfType(_constants.propertyBagPrimitiveDataType.double), buildValueContainerOfType(_constants.dotNetDataTypes.double), buildValueOfType(_constants.propertyBagPrimitiveDataType.decimal), buildValueContainerOfType(_constants.dotNetDataTypes.decimal)],
  dateTimeDataTypes: [buildValueOfType(_constants.propertyBagPrimitiveDataType.dateTime), buildValueContainerOfType(_constants.dotNetDataTypes.dateTime)],
  guidDataTypes: [buildValueOfType(_constants.propertyBagPrimitiveDataType.guid), buildValueContainerOfType(_constants.dotNetDataTypes.guid)],
  isBoolean: function isBoolean(type) {
    return dataTypesChecker.booleanDataTypes.includes(type);
  },
  isInteger: function isInteger(type) {
    return dataTypesChecker.integerDataTypes.includes(type);
  },
  isText: function isText(type) {
    return dataTypesChecker.textDataTypes.includes(type);
  },
  isDecimal: function isDecimal(type) {
    return dataTypesChecker.decimalDataTypes.includes(type);
  },
  isDateTime: function isDateTime(type) {
    return dataTypesChecker.dateTimeDataTypes.includes(type);
  },
  isGuid: function isGuid(type) {
    return dataTypesChecker.guidDataTypes.includes(type);
  }
};
var getPropertyBagFormattedPrimitiveValue = exports.getPropertyBagFormattedPrimitiveValue = function getPropertyBagFormattedPrimitiveValue(propertyBagValue, formatMessage, formatDate, formatTime) {
  if (propertyBagValue === null || propertyBagValue === undefined || !(0, _propertyValidator.isObject)(propertyBagValue)) {
    return propertyBagValue;
  }
  if (propertyBagValue.value === null || propertyBagValue.value === undefined) {
    return null;
  }
  if (dataTypesChecker.isBoolean(propertyBagValue[_constants.serializationTypeKey])) {
    return propertyBagValue.value ? formatMessage(_sharedMessages.default.valueTypeWrapperTrue) : formatMessage(_sharedMessages.default.valueTypeWrapperFalse);
  }
  if (dataTypesChecker.isInteger(propertyBagValue[_constants.serializationTypeKey])) {
    return formatNumber(propertyBagValue.value, 0);
  }
  if (dataTypesChecker.isText(propertyBagValue[_constants.serializationTypeKey])) {
    return propertyBagValue.value.toString();
  }
  if (dataTypesChecker.isDecimal(propertyBagValue[_constants.serializationTypeKey])) {
    return formatNumber(propertyBagValue.value, 2);
  }
  if (dataTypesChecker.isDateTime(propertyBagValue[_constants.serializationTypeKey])) {
    return "".concat(formatDate(propertyBagValue.value), " ").concat(formatTime(propertyBagValue.value));
  }
  if (dataTypesChecker.isGuid(propertyBagValue[_constants.serializationTypeKey])) {
    return (0, _parseHelper.parseGuid)(propertyBagValue.value);
  }

  // we ignore other data types since we have no way to format them
  return null;
};
var formatNumber = exports.formatNumber = function formatNumber(value, precision) {
  return Number(value).toFixed(precision);
};
var fixPropertyBagModifiedModel = exports.fixPropertyBagModifiedModel = function fixPropertyBagModifiedModel(model) {
  var fixProfileOperations = function fixProfileOperations(model) {
    if (Object.prototype.hasOwnProperty.call(model, "profileOperations")) {
      var _model$profileOperati = model.profileOperations,
        profilesToAdd = _model$profileOperati.profilesToAdd,
        profilesToUpdate = _model$profileOperati.profilesToUpdate;
      if (profilesToAdd) {
        var items = Array.isArray(profilesToAdd) ? profilesToAdd : Object.values(profilesToAdd);
        items.forEach(function (pr) {
          fixPropertyBagEmptyValues(pr.propertyBag);
        });
      }
      if (profilesToUpdate) {
        var _items = Array.isArray(profilesToUpdate) ? profilesToUpdate : Object.values(profilesToUpdate);
        _items.forEach(function (pr) {
          fixPropertyBagEmptyValues(pr.propertyBag);
        });
      }
    }
  };
  fixPropertyBagEmptyValues(model.propertyBag);
  fixProfileOperations(model);
  for (var _len = arguments.length, fields = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
    fields[_key - 1] = arguments[_key];
  }
  fields.forEach(function (field) {
    var _model$field;
    var fieldValue = (_model$field = model[field]) == null ? void 0 : _model$field.value;
    if (!fieldValue) return;
    if (Array.isArray(fieldValue)) {
      fieldValue.forEach(function (fieldItem) {
        fixPropertyBagEmptyValues(fieldItem.propertyBag);
        fixProfileOperations(fieldItem);
      });
    } else {
      fixPropertyBagEmptyValues(fieldValue.propertyBag);
    }
  });
};
var fixPropertyBagEmptyValues = exports.fixPropertyBagEmptyValues = function fixPropertyBagEmptyValues(propertyBag) {
  if (!propertyBag) return;
  //we need to pass null to backend to remove attribute instead of empty string
  //in case if attribute has validation for min length and when we will pass empty string, then we will get error from platform
  //in case if DateTime attribute has empty value Backend will transform it to some '01/01/1900' date, so we need to pass null
  var modifiedFields = Object.keys(propertyBag);
  modifiedFields.forEach(function (propertyName) {
    var property = propertyBag[propertyName];
    var propertyValue = property && (0, _propertyValidator.isObject)(property) && Object.prototype.hasOwnProperty.call(property, "value") ? property.value : property;
    if (propertyValue === "") {
      propertyBag[propertyName] = null;
    }
  });
};
var setRequiredBooleansDefault = exports.setRequiredBooleansDefault = function setRequiredBooleansDefault(attributes, propertyBag) {
  var requiredBools = attributes.filter(function (a) {
    return a.isRequired && a.dataType === _constants.attributeDataType.boolean;
  });
  requiredBools.forEach(function (attr) {
    if (!propertyBag[attr.name]) {
      propertyBag[attr.name] = toJsonCargo(attr, false);
    }
  });
};
;
(function () {
  var reactHotLoader = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.default : undefined;
  if (!reactHotLoader) {
    return;
  }
  reactHotLoader.register(customDataType, "customDataType", "/home/vsts/work/1/s/src/utils/propertyBagHelper.js");
  reactHotLoader.register(tieredAttributeTypes, "tieredAttributeTypes", "/home/vsts/work/1/s/src/utils/propertyBagHelper.js");
  reactHotLoader.register(isTieredAttribute, "isTieredAttribute", "/home/vsts/work/1/s/src/utils/propertyBagHelper.js");
  reactHotLoader.register(buildValueOfType, "buildValueOfType", "/home/vsts/work/1/s/src/utils/propertyBagHelper.js");
  reactHotLoader.register(buildValueContainerOfType, "buildValueContainerOfType", "/home/vsts/work/1/s/src/utils/propertyBagHelper.js");
  reactHotLoader.register(createJsonCargo, "createJsonCargo", "/home/vsts/work/1/s/src/utils/propertyBagHelper.js");
  reactHotLoader.register(createTieredTableJsonCargo, "createTieredTableJsonCargo", "/home/vsts/work/1/s/src/utils/propertyBagHelper.js");
  reactHotLoader.register(toJsonCargo, "toJsonCargo", "/home/vsts/work/1/s/src/utils/propertyBagHelper.js");
  reactHotLoader.register(dataTypesChecker, "dataTypesChecker", "/home/vsts/work/1/s/src/utils/propertyBagHelper.js");
  reactHotLoader.register(getPropertyBagFormattedPrimitiveValue, "getPropertyBagFormattedPrimitiveValue", "/home/vsts/work/1/s/src/utils/propertyBagHelper.js");
  reactHotLoader.register(formatNumber, "formatNumber", "/home/vsts/work/1/s/src/utils/propertyBagHelper.js");
  reactHotLoader.register(fixPropertyBagModifiedModel, "fixPropertyBagModifiedModel", "/home/vsts/work/1/s/src/utils/propertyBagHelper.js");
  reactHotLoader.register(fixPropertyBagEmptyValues, "fixPropertyBagEmptyValues", "/home/vsts/work/1/s/src/utils/propertyBagHelper.js");
  reactHotLoader.register(setRequiredBooleansDefault, "setRequiredBooleansDefault", "/home/vsts/work/1/s/src/utils/propertyBagHelper.js");
})();
;
(function () {
  var leaveModule = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.leaveModule : undefined;
  leaveModule && leaveModule(module);
})();
;
(function () {
  var reactHotLoader = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.default : undefined;
  if (!reactHotLoader) {
    return;
  }
  reactHotLoader.register(customDataType, "customDataType", "/home/vsts/work/1/s/src/utils/propertyBagHelper.js");
  reactHotLoader.register(tieredAttributeTypes, "tieredAttributeTypes", "/home/vsts/work/1/s/src/utils/propertyBagHelper.js");
  reactHotLoader.register(isTieredAttribute, "isTieredAttribute", "/home/vsts/work/1/s/src/utils/propertyBagHelper.js");
  reactHotLoader.register(buildValueOfType, "buildValueOfType", "/home/vsts/work/1/s/src/utils/propertyBagHelper.js");
  reactHotLoader.register(buildValueContainerOfType, "buildValueContainerOfType", "/home/vsts/work/1/s/src/utils/propertyBagHelper.js");
  reactHotLoader.register(createJsonCargo, "createJsonCargo", "/home/vsts/work/1/s/src/utils/propertyBagHelper.js");
  reactHotLoader.register(createTieredTableJsonCargo, "createTieredTableJsonCargo", "/home/vsts/work/1/s/src/utils/propertyBagHelper.js");
  reactHotLoader.register(toJsonCargo, "toJsonCargo", "/home/vsts/work/1/s/src/utils/propertyBagHelper.js");
  reactHotLoader.register(dataTypesChecker, "dataTypesChecker", "/home/vsts/work/1/s/src/utils/propertyBagHelper.js");
  reactHotLoader.register(getPropertyBagFormattedPrimitiveValue, "getPropertyBagFormattedPrimitiveValue", "/home/vsts/work/1/s/src/utils/propertyBagHelper.js");
  reactHotLoader.register(formatNumber, "formatNumber", "/home/vsts/work/1/s/src/utils/propertyBagHelper.js");
  reactHotLoader.register(fixPropertyBagModifiedModel, "fixPropertyBagModifiedModel", "/home/vsts/work/1/s/src/utils/propertyBagHelper.js");
  reactHotLoader.register(fixPropertyBagEmptyValues, "fixPropertyBagEmptyValues", "/home/vsts/work/1/s/src/utils/propertyBagHelper.js");
  reactHotLoader.register(setRequiredBooleansDefault, "setRequiredBooleansDefault", "/home/vsts/work/1/s/src/utils/propertyBagHelper.js");
})();
;
(function () {
  var leaveModule = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.leaveModule : undefined;
  leaveModule && leaveModule(module);
})();