'use strict';

var icons = require('@ant-design/icons');
var core = require('@linaria/core');
var index = require('../../core/Calendar/index.js');
var index$2 = require('../../core/Tooltip/index.js');
var index$1 = require('../../core/Typo/index.js');
var getCalendarTitle = require('../common/getCalendarTitle.js');
var useParrotTranslation = require('../../hooks/useParrotTranslation.js');
var cronTime = require('../../utils/cron-time.js');
var dayjs = require('dayjs');
var moment = require('moment');
var React = require('react');

function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }

var dayjs__default = /*#__PURE__*/_interopDefault(dayjs);
var moment__default = /*#__PURE__*/_interopDefault(moment);
var React__default = /*#__PURE__*/_interopDefault(React);

const CronCalendarWrapper = "E_clm1wy1";
const Title = "E_t1tsm00v";
const Control = "E_c167wnad";
const Cell = "E_cxhf7dh";
const CronCalendar = ({
  plans
}) => {
  const {
    t,
    i18n
  } = useParrotTranslation();
  const [value, setValue] = React.useState(moment__default.default());
  const days = React.useMemo(() => {
    const sendAt = [];
    const lastDayOfMonth = dayjs__default.default(value.valueOf()).endOf("month");
    for (const plan of plans) {
      if (plan.empty) {
        continue;
      }
      const cronTime$1 = new cronTime.CronTime(plan.expression, plan.startAt);
      let current = cronTime$1["getNextDateFromWithValidation"](dayjs__default.default());
      while (current.isBefore(lastDayOfMonth)) {
        sendAt.push(current);
        cronTime$1.setLastSendAt(current);
        current = cronTime$1["getNextDateFromWithValidation"](current);
      }
    }
    return sendAt;
  }, [value, plans]);
  return /* @__PURE__ */React__default.default.createElement("div", {
    className: CronCalendarWrapper
  }, /* @__PURE__ */React__default.default.createElement(index, {
    onChange: setValue,
    value,
    fullscreen: false,
    headerRender: ({
      value: value2,
      onChange
    }) => /* @__PURE__ */React__default.default.createElement(React__default.default.Fragment, null, /* @__PURE__ */React__default.default.createElement("div", {
      className: core.cx(Title, index$1.Typo.Label.l4_bold)
    }, t("components.generate_date_preview")), /* @__PURE__ */React__default.default.createElement("div", {
      className: core.cx(Control, index$1.Typo.Label.l4_regular)
    }, /* @__PURE__ */React__default.default.createElement(icons.LeftOutlined, {
      onClick: () => onChange(value2.clone().subtract(1, "month"))
    }), t("components.date_with_year_and_month", {
      year: value2.format("YYYY"),
      month: getCalendarTitle.getCalendarTitle(value2.format("M"), t, i18n),
      interpolation: {
        escapeValue: false
      }
    }), /* @__PURE__ */React__default.default.createElement(icons.RightOutlined, {
      onClick: () => onChange(value2.clone().add(1, "month"))
    }))),
    dateFullCellRender: date => {
      const activeDays = days.filter(d => d.startOf("day").isSame(dayjs__default.default(date.valueOf()).startOf("day")));
      return /* @__PURE__ */React__default.default.createElement(index$2, {
        title: activeDays.length === 0 ? null : activeDays.length === 1 ?
        // TODO pass the callback via props and remove this
        t("components.will_generate_one_reporte_with_date_and_time", {
          date: activeDays[0].format("YYYY/M/D"),
          time: activeDays[0].format("HH:mm"),
          interpolation: {
            escapeValue: false
          }
        }) : /* @__PURE__ */React__default.default.createElement(React__default.default.Fragment, null, t("components.will_generate_report_will_date_and_count", {
          date: activeDays[0].format("YYYY/M/D"),
          count: activeDays.length,
          interpolation: {
            escapeValue: false
          }
        }), /* @__PURE__ */React__default.default.createElement("br", null), activeDays.map((d, idx) => /* @__PURE__ */React__default.default.createElement("div", {
          key: idx
        }, d.format("HH:mm"), /* @__PURE__ */React__default.default.createElement("br", null)))),
        placement: "bottom"
      }, /* @__PURE__ */React__default.default.createElement("div", {
        className: core.cx(Cell, index$1.Typo.Label.l4_regular, activeDays.length > 0 && "active"),
        title: ""
      }, date.date(), /* @__PURE__ */React__default.default.createElement("div", {
        className: "dots"
      }, activeDays.length < 4 && activeDays.map((_, idx) => /* @__PURE__ */React__default.default.createElement("span", {
        className: "dot",
        key: idx
      })), activeDays.length >= 4 && /* @__PURE__ */React__default.default.createElement("div", {
        className: "bar"
      }))));
    }
  }));
};
var CronCalendar$1 = CronCalendar;

module.exports = CronCalendar$1;
