"use strict";

exports.__esModule = true;
exports.default = exports.createFormat = void 0;
var _react = _interopRequireDefault(require("react"));
var _classnames = _interopRequireDefault(require("classnames"));
var _Icon = _interopRequireDefault(require("../DataDisplay/Icon"));
var _reactDatepicker = _interopRequireDefault(require("react-datepicker"));
require("react-datepicker/dist/react-datepicker.css");
var _TimePicker = _interopRequireDefault(require("./TimePicker"));
var _styles = require("@material-ui/core/styles");
var _timezoneHelper = require("../../../utils/timezoneHelper");
var _metadata = require("../../../selectors/metadata");
var _reactRedux = require("react-redux");
var _excluded = ["value", "useTime", "useDate", "onChange", "useTimeZone", "dateFormat", "showTimeZone", "timeInputLabel", "readOnly", "showTimeSelectOnly", "metadata", "timePickerTimeZone", "error", "timeOption"];
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);
})();
function _extends() { return _extends = Object.assign ? Object.assign.bind() : function (n) { for (var e = 1; e < arguments.length; e++) { var t = arguments[e]; for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]); } return n; }, _extends.apply(null, arguments); }
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
function _objectWithoutProperties(e, t) { if (null == e) return {}; var o, r, i = _objectWithoutPropertiesLoose(e, t); if (Object.getOwnPropertySymbols) { var s = Object.getOwnPropertySymbols(e); for (r = 0; r < s.length; r++) o = s[r], t.includes(o) || {}.propertyIsEnumerable.call(e, o) && (i[o] = e[o]); } return i; }
function _objectWithoutPropertiesLoose(r, e) { if (null == r) return {}; var t = {}; for (var n in r) if ({}.hasOwnProperty.call(r, n)) { if (e.includes(n)) continue; t[n] = r[n]; } return t; }
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 useStyles = (0, _styles.makeStyles)(function (theme) {
  return {
    container: {
      display: "flex",
      flexDirection: "column"
    },
    datePickerWrapper: {
      display: "flex",
      width: "auto",
      padding: theme.spacing(0.3, 0.5),
      border: "1px solid ".concat(theme.palette.grey.borders),
      borderRadius: theme.shape.borderRadius,
      alignItems: "center",
      backgroundColor: theme.palette.background.default,
      "&:focus, &:focus-within": {
        borderRadius: theme.shape.borderRadius,
        borderColor: theme.palette.focus,
        boxShadow: "0 0 4px ".concat(theme.palette.focus),
        outline: "none"
      },
      "& .react-datepicker": {
        fontFamily: theme.fontFamily
      },
      "& .react-datepicker__input-time-container": {
        margin: theme.spacing(0.5, 0, 1, 0),
        "& .react-datepicker-time__input-container .react-datepicker-time__input": {
          margin: 0,
          padding: theme.spacing(1)
        }
      },
      "& .react-datepicker-popper": {
        zIndex: 100,
        width: theme.spacing(26)
      },
      "& .react-datepicker__input-container input": {
        fontSize: theme.typography.fontSize,
        color: theme.palette.grey.dark,
        fontFamily: theme.typography.fontFamily,
        width: "inherit",
        border: "none",
        zIndex: 100,
        backgroundColor: "inherit",
        "&:focus": {
          outline: "none"
        }
      },
      "& .react-datepicker__month-container": {
        float: "none"
      },
      "& .react-datepicker__input-time-container .react-datepicker-time__input-container .react-datepicker-time__input": {
        display: "block"
      },
      "& .react-datepicker-time__caption": {
        display: "block"
      },
      "& .react-datepicker__input-time-container .react-datepicker-time__input-container": {
        display: "block"
      },
      "& .react-datepicker__day.disabledDay": {
        color: theme.palette.grey.borders,
        pointerEvents: "none"
      },
      "& .react-datepicker__day.alreadySelectedDay": {
        backgroundColor: theme.palette.background.selectedDate,
        color: theme.palette.primary.contrastText,
        borderRadius: "0.3rem",
        pointerEvents: "none",
        opacity: 0.4
      }
    },
    datePickerWrapperReadOnly: {
      padding: "0",
      border: "none",
      backgroundColor: "inherit"
    },
    disabled: {
      border: "1px solid ".concat(theme.palette.grey.light, " !important"),
      backgroundColor: "".concat(theme.palette.grey.light, " !important")
    },
    calendarIcon: {
      fontSize: theme.fontSize
    },
    datePickerContainer: {
      width: "80%"
    },
    iconContainer: {
      width: "20%",
      display: "flex",
      justifyContent: "flex-end"
    },
    errorInput: {
      border: "".concat(theme.spacing(0.1), " solid ").concat(theme.palette.error.main, " !important"),
      "&:focus, &:focus-within": {
        border: "".concat(theme.spacing(0.1), " solid ").concat(theme.palette.error.main, " !important"),
        boxShadow: "".concat(theme.spacing(0, 0, 0.4), " ").concat(theme.palette.error.main, " !important")
      }
    },
    errorText: {
      marginTop: theme.spacing(0.5),
      color: theme.palette.error.main,
      fontSize: theme.typography.fieldLabelSize,
      float: "left"
    }
  };
});
var createFormat = exports.createFormat = function createFormat(useDate, useTime) {
  if (useDate && !useTime) {
    return "P";
  } else if (useTime && !useDate) {
    return "p";
  } else {
    return "P p";
  }
};
var WrappedDatePicker = function WrappedDatePicker(_ref) {
  var value = _ref.value,
    useTime = _ref.useTime,
    _ref$useDate = _ref.useDate,
    useDate = _ref$useDate === void 0 ? true : _ref$useDate,
    onChange = _ref.onChange,
    _ref$useTimeZone = _ref.useTimeZone,
    useTimeZone = _ref$useTimeZone === void 0 ? false : _ref$useTimeZone,
    dateFormat = _ref.dateFormat,
    showTimeZone = _ref.showTimeZone,
    timeInputLabel = _ref.timeInputLabel,
    readOnly = _ref.readOnly,
    showTimeSelectOnly = _ref.showTimeSelectOnly,
    metadata = _ref.metadata,
    timePickerTimeZone = _ref.timePickerTimeZone,
    error = _ref.error,
    timeOption = _ref.timeOption,
    props = _objectWithoutProperties(_ref, _excluded);
  var classes = useStyles({
    readOnly: readOnly
  });
  var timeZoneName = (0, _timezoneHelper.getTimeZoneByName)(timePickerTimeZone);
  var startDate = value ? timePickerTimeZone && useTimeZone ? (0, _timezoneHelper.convertTimeToLocalTimeZone)(new Date(value), timeZoneName) : new Date(value) : null;
  var disabledCls = (0, _classnames.default)(_defineProperty({}, classes.disabled, props.disabled));
  var localizedTimeZoneName = (0, _reactRedux.useSelector)((0, _metadata.namedLookupLocalizedSelector)("customer", "TimeZone", timePickerTimeZone));
  var updateDate = function updateDate(date, metadata) {
    if (onChange) {
      onChange(useTimeZone && timePickerTimeZone ? (0, _timezoneHelper.convertTimeToOtherTimeZone)(date, timeZoneName) : date, metadata);
    }
  };
  return /*#__PURE__*/_react.default.createElement("div", {
    className: classes.container
  }, /*#__PURE__*/_react.default.createElement("label", {
    className: (0, _classnames.default)(classes.datePickerWrapper, readOnly ? classes.datePickerWrapperReadOnly : null, disabledCls, error ? classes.errorInput : null)
  }, /*#__PURE__*/_react.default.createElement("div", {
    className: classes.datePickerContainer
  }, /*#__PURE__*/_react.default.createElement(_reactDatepicker.default, _extends({}, props, {
    dateFormat: dateFormat || createFormat(useDate, useTime),
    selected: startDate,
    onChange: function onChange(date) {
      return updateDate(date, metadata);
    },
    showTimeInput: useTime != null ? useTime : false,
    useTime: useTime != null ? useTime : false,
    customTimeInput: useTime ? /*#__PURE__*/_react.default.createElement(_TimePicker.default, {
      showTimeZone: showTimeZone,
      requestedTimeZone: localizedTimeZoneName,
      timeOption: timeOption
    }) : null,
    timeInputLabel: timeInputLabel != null ? timeInputLabel : "",
    readOnly: readOnly,
    showTimeSelectOnly: showTimeSelectOnly
  }))), !readOnly ? /*#__PURE__*/_react.default.createElement("div", {
    className: classes.iconContainer
  }, /*#__PURE__*/_react.default.createElement(_Icon.default, {
    className: classes.calendarIcon,
    id: showTimeSelectOnly ? "clock" : "calendar-date"
  })) : null), error && /*#__PURE__*/_react.default.createElement("div", {
    className: classes.errorText
  }, error));
};
__signature__(WrappedDatePicker, "useStyles{classes}\nuseSelector{localizedTimeZoneName}", function () {
  return [useStyles, _reactRedux.useSelector];
});
__signature__(WrappedDatePicker, "useStyles{classes}\nuseSelector{localizedTimeZoneName}", function () {
  return [useStyles, _reactRedux.useSelector];
});
var _default = WrappedDatePicker;
var _default2 = _default;
var _default3 = exports.default = _default2;
;
(function () {
  var reactHotLoader = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.default : undefined;
  if (!reactHotLoader) {
    return;
  }
  reactHotLoader.register(useStyles, "useStyles", "/home/vsts/work/1/s/src/components/MaterialUI/Inputs/DatePicker.js");
  reactHotLoader.register(createFormat, "createFormat", "/home/vsts/work/1/s/src/components/MaterialUI/Inputs/DatePicker.js");
  reactHotLoader.register(WrappedDatePicker, "WrappedDatePicker", "/home/vsts/work/1/s/src/components/MaterialUI/Inputs/DatePicker.js");
  reactHotLoader.register(_default, "default", "/home/vsts/work/1/s/src/components/MaterialUI/Inputs/DatePicker.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(useStyles, "useStyles", "/home/vsts/work/1/s/src/components/MaterialUI/Inputs/DatePicker.js");
  reactHotLoader.register(createFormat, "createFormat", "/home/vsts/work/1/s/src/components/MaterialUI/Inputs/DatePicker.js");
  reactHotLoader.register(WrappedDatePicker, "WrappedDatePicker", "/home/vsts/work/1/s/src/components/MaterialUI/Inputs/DatePicker.js");
  reactHotLoader.register(_default2, "default", "/home/vsts/work/1/s/src/components/MaterialUI/Inputs/DatePicker.js");
})();
;
(function () {
  var leaveModule = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.leaveModule : undefined;
  leaveModule && leaveModule(module);
})();