"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _react = _interopRequireWildcard(require("react"));
var _propTypes = _interopRequireDefault(require("prop-types"));
var _formiga = require("formiga");
var _FIcon = _interopRequireDefault(require("../../../commons/icons/FIcon.cjs"));
var _FInputAddon = require("../../addon/FInputAddon.cjs");
var _reactstrapDatePicker = require("reactstrap-date-picker");
var _inputPropTypes = require("../../props/inputPropTypes.cjs");
var _inputDefaultProps = require("../../props/inputDefaultProps.cjs");
var _isControlled = _interopRequireDefault(require("../../helpers/props/isControlled.cjs"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
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 _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
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); }
var _formatInnerValue = function _formatInnerValue(isoString) {
  var separator = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '/';
  var dateFormat = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : "DD/MM/YYYY";
  var date = isoString ? new Date(isoString) : null;
  if (!date) {
    return '';
  }
  var month = date.getMonth() + 1;
  var day = date.getDate();
  if (dateFormat.match(/MM.DD.YYYY/)) {
    return (month > 9 ? month : "0".concat(month)) + separator + (day > 9 ? day : "0".concat(day)) + separator + date.getFullYear();
  } else if (dateFormat.match(/DD.MM.YYYY/)) {
    return (day > 9 ? day : "0".concat(day)) + separator + (month > 9 ? month : "0".concat(month)) + separator + date.getFullYear();
  } else {
    return date.getFullYear() + separator + (month > 9 ? month : "0".concat(month)) + separator + (day > 9 ? day : "0".concat(day));
  }
};
var FInputDateBase = props => {
  var {
    id,
    name,
    placeholder,
    readOnly,
    autocomplete,
    inputGroupStyle,
    required,
    inputStyle,
    onChange,
    transform,
    showValidity,
    bsSize
  } = props;
  var controlled = (0, _isControlled.default)(props);
  var {
    value,
    defaultValue
  } = props;
  var [innerValue, setInnerValue] = (0, _react.useState)(controlled ? transform.toISO(value) : transform.toISO(defaultValue));
  var input = (0, _formiga.useInput)(_objectSpread(_objectSpread({}, props), {}, {
    transformValue: transform.fromISO,
    checkValue: props.checkValue != undefined ? v => props.checkValue(transform.fromISO(v)) : undefined
  }));
  (0, _react.useEffect)(() => {
    var nInnerValue = controlled ? transform.toISO(value) : transform.toISO(defaultValue);
    //if (innerValue != nInnerValue) {
    setInnerValue(nInnerValue);
    //}
    input.setValue(_formatInnerValue(nInnerValue));
    input.validate();
  }, [/*innerValue,*/value, defaultValue, controlled, transform, input]);
  var handleChange = (value, formattedValue) => {
    setInnerValue(value);
    input.setValue(formattedValue || '');
    input.validate();
    if (onChange != undefined) {
      // TODO Ask RDP to expose event as a onChange() parameter,
      // so we can expose it here too
      onChange(transform.fromISO(value), true, undefined);
    }
  };
  var nInputGroupStyle = _objectSpread(_objectSpread({}, inputGroupStyle), {}, {
    flexWrap: "unset"
  });
  var showValidProps = showValidity == 1 || showValidity == 4 ? {
    valid: input.valid,
    invalid: !input.valid
  } : {};
  return /*#__PURE__*/_react.default.createElement(_FInputAddon.FInputAddon, _extends({}, props, {
    valid: input.valid,
    invalid: !input.valid,
    feedback: input.feedback,
    inputGroupStyle: nInputGroupStyle
  }), /*#__PURE__*/_react.default.createElement(_reactstrapDatePicker.DatePicker, _extends({
    id: id,
    name: name,
    weekStartsOn: 1,
    placeholder: placeholder,
    inputRef: input.ref,
    dateFormat: "DD/MM/YYYY",
    disabled: readOnly,
    required: required,
    autocomplete: autocomplete,
    className: showValidity == 1 || showValidity == 4 ? input.valid ? 'is-valid' : 'is-invalid' : '',
    style: inputStyle,
    value: innerValue,
    onChange: (v, f) => handleChange(v || undefined, f),
    size: bsSize,
    clearButtonElement: /*#__PURE__*/_react.default.createElement(_FIcon.default, {
      icon: "cross"
    })
  }, showValidProps)));
};
FInputDateBase.propTypes = _objectSpread(_objectSpread({}, _inputPropTypes.inputPropTypes), {}, {
  placeholder: _propTypes.default.string,
  autocomplete: _propTypes.default.oneOf(["on", "off"]),
  transform: _propTypes.default.object
});
FInputDateBase.defaultProps = _objectSpread(_objectSpread({}, _inputDefaultProps.inputDefaultProps), {}, {
  icon: 'calendar',
  inputStyle: {} // invalidate the r-d-p width default
});
var _default = FInputDateBase;
exports.default = _default;