import _slicedToArray from '@babel/runtime/helpers/slicedToArray';
import { useState, useEffect } from 'react';
import { noop } from '@bigbinary/neeto-cist';
import Button from '@bigbinary/neetoui/Button';
import Modal from '@bigbinary/neetoui/Modal';
import Typography from '@bigbinary/neetoui/Typography';
import { useTranslation, Trans } from 'react-i18next';
import { useMutation } from '@tanstack/react-query';
import axios from 'axios';
import dayjs from 'dayjs';
import { globalProps as globalProps$1 } from '@bigbinary/neeto-commons-frontend/initializers';
import { jsxs, jsx } from 'react/jsx-runtime';

var timeZoneUrl = "api/v1/time_zone";
var updateTimeZone = function updateTimeZone(payload) {
  return axios.patch(timeZoneUrl, payload);
};
var timeZoneApi = {
  updateTimeZone: updateTimeZone
};

var useUpdateTimeZone = function useUpdateTimeZone() {
  return useMutation({
    mutationFn: timeZoneApi.updateTimeZone,
    onSuccess: function onSuccess() {
      return window.location.reload();
    }
  });
};

var getBrowserTimeZone = function getBrowserTimeZone() {
  return dayjs.tz.guess();
};
var getUserTimeZone = function getUserTimeZone() {
  var _globalProps$user;
  return (_globalProps$user = globalProps$1.user) === null || _globalProps$user === void 0 ? void 0 : _globalProps$user.timeZone;
};
var checkIfTwoTimeZonesAreSame = function checkIfTwoTimeZonesAreSame(timeZone1, timeZone2) {
  return (
    // eslint-disable-next-line @bigbinary/neeto/use-standard-date-time-formats
    dayjs().tz(timeZone1).format("Z") === dayjs().tz(timeZone2).format("Z")
  );
};

var TimezoneMismatchModal = function TimezoneMismatchModal(_ref) {
  var _ref$isOpen = _ref.isOpen,
    isOpen = _ref$isOpen === void 0 ? false : _ref$isOpen,
    _ref$onClose = _ref.onClose,
    onClose = _ref$onClose === void 0 ? noop : _ref$onClose;
  var _useTranslation = useTranslation(),
    t = _useTranslation.t;
  var _useState = useState(false),
    _useState2 = _slicedToArray(_useState, 2),
    isTimezoneModalOpen = _useState2[0],
    setIsTimezoneModalOpen = _useState2[1];
  var _useUpdateTimeZone = useUpdateTimeZone(),
    isPending = _useUpdateTimeZone.isPending,
    updateTimeZone = _useUpdateTimeZone.mutate;
  var isUserAndBrowserTimeZoneSame = checkIfTwoTimeZonesAreSame(getUserTimeZone(), getBrowserTimeZone());
  var handleUpdateTimeZone = function handleUpdateTimeZone() {
    var payload = {
      timeZone: getBrowserTimeZone()
    };
    updateTimeZone(payload, {
      onSuccess: function onSuccess() {
        setIsTimezoneModalOpen(false);
        onClose();
      }
    });
  };
  var handleCancel = function handleCancel() {
    setIsTimezoneModalOpen(false);
    onClose();
  };
  useEffect(function () {
    if (!(process.env.RAILS_ENV !== "heroku")) return;
    setIsTimezoneModalOpen(!isUserAndBrowserTimeZoneSame);
  }, []);
  return /*#__PURE__*/jsxs(Modal, {
    closeOnOutsideClick: false,
    isOpen: isTimezoneModalOpen || isOpen,
    size: "large",
    onClose: handleCancel,
    children: [/*#__PURE__*/jsx(Modal.Header, {
      children: /*#__PURE__*/jsx(Typography, {
        style: "h2",
        weight: "normal",
        children: t("neetoMolecules.alert.timezone.title")
      })
    }), /*#__PURE__*/jsx(Modal.Body, {
      children: /*#__PURE__*/jsxs("span", {
        className: "flex flex-col gap-y-4",
        children: [/*#__PURE__*/jsx(Typography, {
          component: "span",
          style: "body2",
          children: t("neetoMolecules.alert.timezone.message", {
            entity: globalProps.appName
          })
        }), /*#__PURE__*/jsx(Typography, {
          component: "span",
          style: "h4",
          children: /*#__PURE__*/jsx(Trans, {
            i18nKey: "neetoMolecules.alert.timezone.userTimezone",
            values: {
              entity: getUserTimeZone()
            }
          })
        }), /*#__PURE__*/jsx(Typography, {
          component: "span",
          style: "h4",
          children: /*#__PURE__*/jsx(Trans, {
            i18nKey: "neetoMolecules.alert.timezone.browserTimezone",
            values: {
              entity: getBrowserTimeZone()
            }
          })
        })]
      })
    }), /*#__PURE__*/jsxs(Modal.Footer, {
      className: "flex items-center gap-x-2",
      children: [/*#__PURE__*/jsx(Button, {
        "data-testid": "update-timezone-button",
        disabled: isPending,
        label: t("neetoMolecules.alert.timezone.updateProfile"),
        loading: isPending,
        style: "primary",
        onClick: handleUpdateTimeZone
      }), /*#__PURE__*/jsx(Button, {
        label: t("neetoMolecules.common.actions.cancel"),
        style: "text",
        onClick: handleCancel
      })]
    })]
  });
};

export { TimezoneMismatchModal as default };
//# sourceMappingURL=TimezoneMismatchModal.js.map
