import React, { useMemo } from "react";
import classnames from "classnames";
import { SelectDateTarget } from "./SelectDateTarget";
import { StyledSelectDate, StyledSelectDateButton } from "./Styles";
import { formatDate } from "../../utils/dateUtils";
import { DatePickerV2, toMoment } from "../DatePicker/DatePickerV2";
const isDayInRange = (day, data) => {
  if (data.repeating) {
    const from = new Date(day.getFullYear(), data.from.getMonth(), data.from.getDate());
    const to = new Date(day.getFullYear(), data.to.getMonth(), data.to.getDate());
    return day >= from && day <= to;
  }
  return day >= data.from && day <= data.to;
};
export const SelectDateV2 = _ref => {
  let {
    changeMode: mode = "instant",
    trigger: labelType = "text",
    saveButtonText = "Save",
    cancelButtonText = "Cancel",
    clearButtonText = "Clear",
    modalHeaderText = "Discard changes?",
    modalDiscardMessage = "All unsaved changes will be lost.",
    modalDiscardBtnText = "OK",
    modalCancelBtnText = "Cancel",
    onDayClick,
    onSave,
    onCancel,
    onToggleDatePicker,
    required: dateRequired = false,
    longDateFormat = false,
    defaultShowDatePicker = false,
    firstDayOfWeek = 0,
    selectedDays,
    selectionMode = "custom",
    menuClassName,
    targetClassName,
    icon,
    defaultLabelText = "Set...",
    targetTextClassName,
    targetIconClassName,
    backgroundElementClass,
    dateFormat,
    defaultMonth,
    daysToModify = [],
    weekends = [],
    weekendLabel = "Weekend",
    nonWorkingDaysOfWeek = [],
    nonWorkingDaysOfWeekLabel = "Unavailable",
    weekendIsSelectable = false,
    tooltipText,
    popperTooltipClassName,
    popperClassName,
    popperTooltipStyle,
    position,
    disableDaysBefore,
    enableYearPicker,
    disableAnimations
  } = _ref;
  const labelText = useMemo(() => {
    if (!selectedDays) {
      return defaultLabelText;
    } else if (!selectedDays.from && !selectedDays.to) {
      return defaultLabelText;
    } else {
      const formattedEndDate = selectedDays.to ? formatDate(toMoment(selectedDays.to), dateFormat, longDateFormat) : "";
      const formattedStartDate = selectedDays.from ? formatDate(toMoment(selectedDays == null ? void 0 : selectedDays.from), dateFormat, longDateFormat) : "";
      if (selectedDays.to && selectedDays.from && toMoment(selectedDays.from).isSame(toMoment(selectedDays.to))) {
        return formattedEndDate;
      }
      return formattedStartDate + " - " + formattedEndDate;
    }
  }, [selectedDays, dateFormat, defaultLabelText, longDateFormat]);
  const modifiers = useMemo(() => {
    const userAvailabilities = daysToModify.filter(data => data.type === "user_day_off");
    const globalDaysOff = daysToModify.filter(data => data.type === "global_day_off");
    const selectableGlobalDaysOff = daysToModify.filter(data => data.type === "selectable_global_day_off");
    return {
      userAvailability: day => {
        const currentDay = new Date(day.getFullYear(), day.getMonth(), day.getDate());
        const matchedData = userAvailabilities.find(data => isDayInRange(currentDay, data));
        return {
          matched: !!matchedData,
          title: matchedData ? matchedData.title : null
        };
      },
      weekend: day => {
        const isWeekend = weekends.includes(day.getDay());
        return {
          matched: isWeekend,
          title: isWeekend ? weekendLabel : null
        };
      },
      nonWorkingDay: day => {
        if (weekends.includes(day.getDay())) {
          return {
            matched: true,
            title: ""
          };
        }
        const nonWorkingDays = [...selectableGlobalDaysOff, ...globalDaysOff];
        const currentDay = new Date(day.getFullYear(), day.getMonth(), day.getDate());
        const matchedData = nonWorkingDays.find(data => isDayInRange(currentDay, data));
        return {
          matched: !!matchedData,
          title: matchedData ? matchedData.title : null
        };
      },
      nonWorkingDaysOfWeek: day => {
        const isNonWorkingDay = nonWorkingDaysOfWeek.includes(day.getDay());
        return {
          matched: isNonWorkingDay,
          title: isNonWorkingDay ? nonWorkingDaysOfWeekLabel : null
        };
      }
    };
  }, [daysToModify, nonWorkingDaysOfWeek, nonWorkingDaysOfWeekLabel, weekendLabel, weekends]);
  const renderTargetEl = useMemo(() => {
    if (labelType === "icon" && icon) {
      return /*#__PURE__*/React.createElement(SelectDateTarget, {
        icon: icon,
        title: tooltipText,
        targetIconClassName: targetIconClassName,
        popperTooltipClassName: popperTooltipClassName,
        popperTooltipStyle: popperTooltipStyle
      });
    }
    if (typeof labelType === "function") {
      return labelType(labelText);
    }
    return /*#__PURE__*/React.createElement(StyledSelectDateButton, {
      type: "button",
      className: classnames("date-picker-target", targetClassName)
    }, /*#__PURE__*/React.createElement("span", {
      className: targetTextClassName
    }, labelText));
  }, [icon, labelText, labelType, popperTooltipClassName, popperTooltipStyle, targetClassName, targetIconClassName, targetTextClassName, tooltipText]);
  return /*#__PURE__*/React.createElement(StyledSelectDate, {
    className: "select-date"
  }, labelType !== "inline" ? /*#__PURE__*/React.createElement(DatePickerV2, {
    target: renderTargetEl,
    position: position,
    menuClassName: menuClassName,
    popperClassName: popperClassName,
    month: defaultMonth,
    instant: mode === "instant",
    mode: selectionMode,
    required: dateRequired,
    selected: selectedDays,
    firstDayOfWeek: firstDayOfWeek,
    saveLabel: saveButtonText,
    cancelLabel: cancelButtonText,
    clearLabel: clearButtonText,
    onSave: onSave,
    onClose: onCancel,
    modifiers: modifiers,
    modalHeaderText: modalHeaderText,
    modalDiscardMessage: modalDiscardMessage,
    modalDiscardBtnText: modalDiscardBtnText,
    modalCancelBtnText: modalCancelBtnText,
    disabledDaysBefore: disableDaysBefore,
    backgroundElementClass: backgroundElementClass,
    disabled: weekendIsSelectable ? [] : [0, 6],
    popperTooltipClassName: popperTooltipClassName,
    popperTooltipStyle: popperTooltipStyle,
    open: defaultShowDatePicker,
    onCalendarToggle: onToggleDatePicker,
    onDayClick: onDayClick,
    enableConfirmModal: mode === "atomic" && !dateRequired,
    disableYearPicker: !enableYearPicker,
    disableAnimations: disableAnimations,
    showControls: true
  }) : /*#__PURE__*/React.createElement(DatePickerV2, {
    month: defaultMonth,
    instant: mode === "instant",
    mode: selectionMode,
    required: dateRequired,
    selected: selectedDays,
    firstDayOfWeek: firstDayOfWeek,
    saveLabel: saveButtonText,
    cancelLabel: cancelButtonText,
    clearLabel: clearButtonText,
    disabledDaysBefore: disableDaysBefore,
    popperTooltipClassName: popperTooltipClassName,
    popperTooltipStyle: popperTooltipStyle,
    disabled: weekendIsSelectable ? [] : [0, 6],
    onSave: onSave,
    onChange: onSave,
    modifiers: modifiers,
    onDayClick: onDayClick,
    disableYearPicker: !enableYearPicker,
    disableAnimations: disableAnimations,
    showControls: true,
    open: true
  }));
};
SelectDateV2.displayName = "SelectDateV2";
//# sourceMappingURL=SelectDateV2.js.map