import _objectSpread from "@babel/runtime/helpers/objectSpread2";
import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
import * as React from 'react';
import { AdapterDayjs } from '@mui/x-date-pickers/AdapterDayjs';
import { LocalizationProvider } from '@mui/x-date-pickers';
import { DatePicker, fiFI, svSE } from '@mui/x-date-pickers';
import { defineMessage, FormattedMessage, useIntl } from 'react-intl';
import dayjs from 'dayjs';
import "dayjs/locale/fi";
import "dayjs/locale/en-gb";
import "dayjs/locale/sv";
import { add, sub } from 'date-fns';
import { getAriaLabel } from '../../../utils';
import Button from '@material-ui/core/Button';
import { makeStyles } from '@material-ui/core/styles';
import { useEffect } from 'react';
import { jsx as _jsx } from "react/jsx-runtime";
import { Fragment as _Fragment } from "react/jsx-runtime";
import { jsxs as _jsxs } from "react/jsx-runtime";
var useStyles = makeStyles(function (theme) {
  return {
    button: _defineProperty({
      marginRight: theme.spacing(1),
      marginLeft: theme.spacing(1)
    }, theme.breakpoints.down('xs'), {
      marginTop: theme.spacing(1),
      marginLeft: '0px'
    }),
    dateBox: _defineProperty({
      display: 'flex',
      alignItems: 'center'
    }, theme.breakpoints.down('xs'), {
      flexDirection: 'column',
      alignItems: 'start'
    })
  };
});

var getDefaultValue = function getDefaultValue(_ref) {
  var type = _ref.type,
      defaultValue = _ref.defaultValue,
      limitType = _ref.limitType,
      limitAmount = _ref.limitAmount;

  switch (type) {
    case 'noDefault':
      return null;

    case 'today':
      return dayjs();

    case 'fixed':
      return dayjs(defaultValue);

    case 'limit':
      return limitAmount < 0 ? beforeDay({
        type: limitType,
        numberValue: limitAmount * -1
      }) : afterDay({
        type: limitType,
        numberValue: limitAmount
      });
  }

  return null;
};

var beforeDay = function beforeDay(disableBefore) {
  if ((disableBefore === null || disableBefore === void 0 ? void 0 : disableBefore.type) && (disableBefore === null || disableBefore === void 0 ? void 0 : disableBefore.type) !== 'noValue') {
    return dayjs(disableBefore.type !== 'date' ? sub(new Date(), _defineProperty({}, disableBefore.type, disableBefore.numberValue)) : disableBefore.dateValue);
  } else {
    return null;
  }
};

var afterDay = function afterDay(disableAfter) {
  if ((disableAfter === null || disableAfter === void 0 ? void 0 : disableAfter.type) && (disableAfter === null || disableAfter === void 0 ? void 0 : disableAfter.type) !== 'noValue') {
    return dayjs(disableAfter.type !== 'date' ? add(new Date(), _defineProperty({}, disableAfter.type, disableAfter.numberValue)) : disableAfter.dateValue);
  } else {
    return null;
  }
};

function BasicDatePicker(props) {
  var _props$options$dateFo, _props$options;

  var onChange = props.onChange,
      options = props.options;

  var _React$useState = React.useState(null),
      _React$useState2 = _slicedToArray(_React$useState, 2),
      value = _React$useState2[0],
      setValue = _React$useState2[1];

  var intl = useIntl();
  var locale = intl.locale;

  var _options$element = options === null || options === void 0 ? void 0 : options.element,
      disableBefore = _options$element.disableBefore,
      disableAfter = _options$element.disableAfter;

  var classes = useStyles();
  var ariaLabel = getAriaLabel(props.label, props.options, props.required, intl.formatMessage({
    id: "l4R23W",
    defaultMessage: [{
      "type": 0,
      "value": "Pakollinen kenttä"
    }]
  }));

  var handleLocaleText = function handleLocaleText() {
    if (locale === 'fi-FI') {
      var _fiFI$components, _fiFI$components$MuiL, _fiFI$components$MuiL2;

      return fiFI === null || fiFI === void 0 ? void 0 : (_fiFI$components = fiFI.components) === null || _fiFI$components === void 0 ? void 0 : (_fiFI$components$MuiL = _fiFI$components.MuiLocalizationProvider) === null || _fiFI$components$MuiL === void 0 ? void 0 : (_fiFI$components$MuiL2 = _fiFI$components$MuiL.defaultProps) === null || _fiFI$components$MuiL2 === void 0 ? void 0 : _fiFI$components$MuiL2.localeText;
    } else if (locale === 'sv-SE') {
      var _svSE$components, _svSE$components$MuiL, _svSE$components$MuiL2;

      return svSE === null || svSE === void 0 ? void 0 : (_svSE$components = svSE.components) === null || _svSE$components === void 0 ? void 0 : (_svSE$components$MuiL = _svSE$components.MuiLocalizationProvider) === null || _svSE$components$MuiL === void 0 ? void 0 : (_svSE$components$MuiL2 = _svSE$components$MuiL.defaultProps) === null || _svSE$components$MuiL2 === void 0 ? void 0 : _svSE$components$MuiL2.localeText;
    }
  };

  useEffect(function () {
    setValue((props === null || props === void 0 ? void 0 : props.value) ? dayjs(props === null || props === void 0 ? void 0 : props.value) : options.dateDefault ? getDefaultValue(options.dateDefault) : null);
  }, []);
  useEffect(function () {
    setTimeout(function () {
      onChange(value === null || value === void 0 ? void 0 : value.format('YYYY-MM-DD'));
    });
  }, [value]);
  return /*#__PURE__*/_jsx(LocalizationProvider, {
    dateAdapter: AdapterDayjs,
    adapterLocale: locale.split('-')[0].toString(),
    localeText: handleLocaleText(),
    children: /*#__PURE__*/_jsxs("div", {
      className: classes.dateBox,
      children: [/*#__PURE__*/_jsx(DatePicker, _objectSpread(_objectSpread({}, (options === null || options === void 0 ? void 0 : options.dateDefault) ? {
        defaultValue: getDefaultValue(options.dateDefault)
      } : {}), {}, {
        value: value,
        label: intl.formatMessage({
          id: "xLN8SB",
          defaultMessage: [{
            "type": 0,
            "value": "Päivämäärä"
          }]
        }),
        minDate: beforeDay(disableBefore),
        maxDate: afterDay(disableAfter),
        sx: {
          maxWidth: 200,
          "& .MuiInputBase-input.Mui-disabled": {
            WebkitTextFillColor: "#000000"
          }
        },
        arialabel: ariaLabel,
        format: (_props$options$dateFo = props === null || props === void 0 ? void 0 : (_props$options = props.options) === null || _props$options === void 0 ? void 0 : _props$options.dateFormat) !== null && _props$options$dateFo !== void 0 ? _props$options$dateFo : 'D.M.YYYY',
        onChange: setValue,
        slotProps: {
          textField: {
            disabled: true
          }
        }
      })), value ? /*#__PURE__*/_jsx(Button, {
        variant: "contained",
        color: "secondary",
        size: "small",
        className: classes.button,
        onClick: function onClick(value) {
          return value && props.list ? onChange([null]) : value && setValue(null);
        },
        children: /*#__PURE__*/_jsx(FormattedMessage, {
          id: "fVRBN+",
          defaultMessage: [{
            "type": 0,
            "value": "Tyhjennä"
          }]
        })
      }) : /*#__PURE__*/_jsx(_Fragment, {})]
    })
  });
}

export default (function (_ref2) {
  var _reviewProps$dateForm;

  var _ref2$config = _ref2.config,
      disableBefore = _ref2$config.disableBefore,
      disableAfter = _ref2$config.disableAfter,
      dateDefault = _ref2$config.dateDefault,
      reviewProps = _ref2.reviewProps;
  return {
    schema: {
      format: 'date',
      type: 'string'
    },
    uiSchema: {
      'ui:widget': BasicDatePicker,
      'ui:options': {
        disableBefore: disableBefore,
        disableAfter: disableAfter,
        dateDefault: dateDefault,
        dateFormat: reviewProps === null || reviewProps === void 0 ? void 0 : (_reviewProps$dateForm = reviewProps.dateFormat) === null || _reviewProps$dateForm === void 0 ? void 0 : _reviewProps$dateForm.replaceAll('d', 'D')
      }
    }
  };
});
export var name = defineMessage({
  id: "xLN8SB",
  defaultMessage: [{
    "type": 0,
    "value": "Päivämäärä"
  }]
});
export var elementType = 'field';
//# sourceMappingURL=index.js.map