"use strict";Object.defineProperty(exports, "__esModule", {value: true});

var _chunkO62GMM6Njs = require('./chunk-O62GMM6N.js');

// tmp-babel/activity-log/web/ActivityLogComponent/DateFilterChip/index.js
var _react = require('react');
var _drawers = require('@baseapp-frontend/design-system/components/web/drawers');
var _utils = require('@baseapp-frontend/utils');
var _iconsmaterial = require('@mui/icons-material');
var _material = require('@mui/material');
var _luxon = require('luxon');
var _jsxruntime = require('react/jsx-runtime');
function _slicedToArray(r, e) {
  return _arrayWithHoles(r) || _iterableToArrayLimit(r, e) || _unsupportedIterableToArray(r, e) || _nonIterableRest();
}
function _nonIterableRest() {
  throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
}
function _unsupportedIterableToArray(r, a) {
  if (r) {
    if ("string" == typeof r) return _arrayLikeToArray(r, a);
    var t = {}.toString.call(r).slice(8, -1);
    return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0;
  }
}
function _arrayLikeToArray(r, a) {
  (null == a || a > r.length) && (a = r.length);
  for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e];
  return n;
}
function _iterableToArrayLimit(r, l) {
  var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"];
  if (null != t) {
    var e, n, i, u, a = [], f = true, o = false;
    try {
      if (i = (t = t.call(r)).next, 0 === l) {
        if (Object(t) !== t) return;
        f = false;
      } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = true) ;
    } catch (r2) {
      o = true, n = r2;
    } finally {
      try {
        if (!f && null != t["return"] && (u = t["return"](), Object(u) !== u)) return;
      } finally {
        if (o) throw n;
      }
    }
    return a;
  }
}
function _arrayWithHoles(r) {
  if (Array.isArray(r)) return r;
}
var DateFilterChip = function DateFilterChip2(_ref) {
  var fetchParameters = _ref.fetchParameters, executeRefetch = _ref.executeRefetch;
  var _useState = _react.useState.call(void 0, false), _useState2 = _slicedToArray(_useState, 2), drawerOpen = _useState2[0], setDrawerOpen = _useState2[1];
  var _useState3 = _react.useState.call(void 0, null), _useState4 = _slicedToArray(_useState3, 2), anchorEl = _useState4[0], setAnchorEl = _useState4[1];
  var isMobile = _material.useMediaQuery.call(void 0, function(theme) {
    return theme.breakpoints.down("sm");
  });
  var handleDrawerOpen = function handleDrawerOpen2() {
    return setDrawerOpen(true);
  };
  var handleDrawerClose = function handleDrawerClose2() {
    return setDrawerOpen(false);
  };
  var handleMenuClose = function handleMenuClose2() {
    return setAnchorEl(null);
  };
  var createdFrom = fetchParameters.createdFrom, createdTo = fetchParameters.createdTo;
  var hasDateSelected = createdFrom || createdTo;
  var parseDate = function parseDate2(date) {
    return date ? _luxon.DateTime.fromFormat(date, _utils.DATE_FORMAT.api) : null;
  };
  var formatLabelDate = function formatLabelDate2(date) {
    return date ? _utils.formatDate.call(void 0, date, {
      toFormat: _utils.DATE_FORMAT[3]
    }) : null;
  };
  var labelRender = function labelRender2() {
    var fromDate = formatLabelDate(createdFrom);
    var toDate = formatLabelDate(createdTo);
    if (createdFrom && createdTo) return "".concat(fromDate, " - ").concat(toDate);
    if (createdFrom) return "From ".concat(fromDate);
    if (createdTo) return "Until ".concat(toDate);
    return "Period";
  };
  var handleOnClickOnChip = function handleOnClickOnChip2(event) {
    if (isMobile) {
      handleDrawerOpen();
    } else {
      setAnchorEl(event.currentTarget);
    }
  };
  var renderDateFilterComponent = function renderDateFilterComponent2(onClose) {
    return /* @__PURE__ */ _jsxruntime.jsxs.call(void 0, _jsxruntime.Fragment, {
      children: [/* @__PURE__ */ _jsxruntime.jsx.call(void 0, _material.Typography, {
        variant: "subtitle1",
        m: 4,
        align: "center",
        children: "Period"
      }), /* @__PURE__ */ _jsxruntime.jsx.call(void 0, _material.Divider, {
        sx: {
          marginBottom: 2
        }
      }), /* @__PURE__ */ _jsxruntime.jsx.call(void 0, _chunkO62GMM6Njs.DateFilterComponent_default, {
        createdFrom: parseDate(createdFrom),
        createdTo: parseDate(createdTo),
        executeRefetch,
        onApply: onClose,
        onClearFilter: onClose
      })]
    });
  };
  return /* @__PURE__ */ _jsxruntime.jsxs.call(void 0, _jsxruntime.Fragment, {
    children: [/* @__PURE__ */ _jsxruntime.jsx.call(void 0, _material.Chip, {
      label: /* @__PURE__ */ _jsxruntime.jsxs.call(void 0, _material.Box, {
        sx: {
          display: "flex",
          alignItems: "center",
          gap: 1
        },
        children: [/* @__PURE__ */ _jsxruntime.jsx.call(void 0, "span", {
          children: labelRender()
        }), /* @__PURE__ */ _jsxruntime.jsx.call(void 0, _iconsmaterial.KeyboardArrowDown, {
          color: "action"
        })]
      }),
      onClick: handleOnClickOnChip,
      variant: hasDateSelected ? "filled" : "soft",
      color: "default"
    }), isMobile ? /* @__PURE__ */ _jsxruntime.jsx.call(void 0, _drawers.SwipeableDrawer, {
      globalHeight: "auto",
      open: drawerOpen,
      onClose: handleDrawerClose,
      children: renderDateFilterComponent(handleDrawerClose)
    }) : /* @__PURE__ */ _jsxruntime.jsx.call(void 0, _material.Menu, {
      anchorEl,
      open: Boolean(anchorEl),
      onClose: handleMenuClose,
      children: /* @__PURE__ */ _jsxruntime.jsx.call(void 0, _material.Box, {
        padding: 2,
        children: renderDateFilterComponent(handleMenuClose)
      })
    })]
  });
};
var DateFilterChip_default = DateFilterChip;



exports.DateFilterChip_default = DateFilterChip_default;
//# sourceMappingURL=chunk-Y32DFJ3G.js.map