import { LeftOutlined, RightOutlined } from '@ant-design/icons';
import { cx } from '@linaria/core';
import Calendar from '../../core/Calendar/index.js';
import Tooltip from '../../core/Tooltip/index.js';
import { Typo } from '../../core/Typo/index.js';
import { getCalendarTitle } from '../common/getCalendarTitle.js';
import useParrotTranslation from '../../hooks/useParrotTranslation.js';
import { CronTime } from '../../utils/cron-time.js';
import dayjs from 'dayjs';
import moment from 'moment';
import React__default, { useState, useMemo } from 'react';

const CronCalendarWrapper = "E_4100_0_7_clm1wy1";
const Title = "E_4100_0_7_t1tsm00v";
const Control = "E_4100_0_7_c167wnad";
const Cell = "E_4100_0_7_cxhf7dh";
const CronCalendar = ({
  plans
}) => {
  const {
    t,
    i18n
  } = useParrotTranslation();
  const [value, setValue] = useState(moment());
  const days = useMemo(() => {
    const sendAt = [];
    const lastDayOfMonth = dayjs(value.valueOf()).endOf("month");
    for (const plan of plans) {
      if (plan.empty) {
        continue;
      }
      const cronTime = new CronTime(plan.expression, plan.startAt);
      let current = cronTime["getNextDateFromWithValidation"](dayjs());
      while (current.isBefore(lastDayOfMonth)) {
        sendAt.push(current);
        cronTime.setLastSendAt(current);
        current = cronTime["getNextDateFromWithValidation"](current);
      }
    }
    return sendAt;
  }, [value, plans]);
  return /* @__PURE__ */React__default.createElement("div", {
    className: CronCalendarWrapper
  }, /* @__PURE__ */React__default.createElement(Calendar, {
    onChange: setValue,
    value,
    fullscreen: false,
    headerRender: ({
      value: value2,
      onChange
    }) => /* @__PURE__ */React__default.createElement(React__default.Fragment, null, /* @__PURE__ */React__default.createElement("div", {
      className: cx(Title, Typo.Label.l4_bold)
    }, t("components.generate_date_preview")), /* @__PURE__ */React__default.createElement("div", {
      className: cx(Control, Typo.Label.l4_regular)
    }, /* @__PURE__ */React__default.createElement(LeftOutlined, {
      onClick: () => onChange(value2.clone().subtract(1, "month"))
    }), t("components.date_with_year_and_month", {
      year: value2.format("YYYY"),
      month: getCalendarTitle(value2.format("M"), t, i18n),
      interpolation: {
        escapeValue: false
      }
    }), /* @__PURE__ */React__default.createElement(RightOutlined, {
      onClick: () => onChange(value2.clone().add(1, "month"))
    }))),
    dateFullCellRender: date => {
      const activeDays = days.filter(d => d.startOf("day").isSame(dayjs(date.valueOf()).startOf("day")));
      return /* @__PURE__ */React__default.createElement(Tooltip, {
        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.createElement(React__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.createElement("br", null), activeDays.map((d, idx) => /* @__PURE__ */React__default.createElement("div", {
          key: idx
        }, d.format("HH:mm"), /* @__PURE__ */React__default.createElement("br", null)))),
        placement: "bottom"
      }, /* @__PURE__ */React__default.createElement("div", {
        className: cx(Cell, Typo.Label.l4_regular, activeDays.length > 0 && "active"),
        title: ""
      }, date.date(), /* @__PURE__ */React__default.createElement("div", {
        className: "dots"
      }, activeDays.length < 4 && activeDays.map((_, idx) => /* @__PURE__ */React__default.createElement("span", {
        className: "dot",
        key: idx
      })), activeDays.length >= 4 && /* @__PURE__ */React__default.createElement("div", {
        className: "bar"
      }))));
    }
  }));
};
var CronCalendar$1 = CronCalendar;

export { CronCalendar$1 as default };
