function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _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(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
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 = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
import React, { useMemo, useCallback, useState } from 'react';
import dayjs from 'dayjs';
import classNames from 'classnames';
import { Modal } from 'antd';
import { DateCalendar } from '@pisell/date-picker';
import ChevronLeft from '@pisell/icon/es/ChevronLeft';
import ChevronRight from '@pisell/icon/es/ChevronRight';
import { locales } from '@pisell/utils';
import useEngineContext from "../../hooks/useEngineContext";
import pisellReservationScheduleLocales from "./locales";
import "./PisellReservationSchedule.less";
var I18N_TODAY = 'pisell-reservation-schedule.today';
var I18N_PREV = 'pisell-reservation-schedule.prev';
var I18N_NEXT = 'pisell-reservation-schedule.next';
var I18N_PICK_DATE = 'pisell-reservation-schedule.pick-date';
var I18N_PICK_DATE_TITLE = 'pisell-reservation-schedule.pick-date-title';
function parseDay(input, fallback) {
  if (input == null) return fallback.startOf('day');
  var d = dayjs(input);
  return (d.isValid() ? d : fallback).startOf('day');
}
function toDayjsLocale(engineLocale) {
  if (!engineLocale || engineLocale === 'en') return 'en';
  if (engineLocale === 'zh-HK') return 'zh-hk';
  return 'zh-cn';
}

/**
 * 营业日导航条：上一日、中间日期文案、下一日（深色条样式，适配预约顶栏）
 */
export function PisellReservationSchedule(props) {
  var _context$engine;
  var valueProp = props.value,
    onChange = props.onChange,
    defaultValue = props.defaultValue,
    minDateProp = props.minDate,
    maxDateProp = props.maxDate,
    _props$navigationLock = props.navigationLocked,
    navigationLocked = _props$navigationLock === void 0 ? false : _props$navigationLock,
    className = props.className,
    style = props.style;
  var context = useEngineContext();
  var engineLocale = (context === null || context === void 0 || (_context$engine = context.engine) === null || _context$engine === void 0 || (_context$engine = _context$engine.props) === null || _context$engine === void 0 ? void 0 : _context$engine.locale) || 'en';
  locales.init(pisellReservationScheduleLocales, engineLocale);
  var today = useMemo(function () {
    return dayjs().startOf('day');
  }, []);
  var isControlled = valueProp !== undefined;
  var _useState = useState(function () {
      return parseDay(valueProp !== null && valueProp !== void 0 ? valueProp : defaultValue, today);
    }),
    _useState2 = _slicedToArray(_useState, 2),
    internal = _useState2[0],
    setInternal = _useState2[1];
  var value = isControlled ? parseDay(valueProp, today) : internal;
  var minDate = useMemo(function () {
    return minDateProp != null ? parseDay(minDateProp, today) : undefined;
  }, [minDateProp, today]);
  var maxDate = useMemo(function () {
    return maxDateProp != null ? parseDay(maxDateProp, today) : undefined;
  }, [maxDateProp, today]);
  var emit = useCallback(function (next) {
    var d = next.startOf('day');
    if (minDate && d.isBefore(minDate)) d = minDate;
    if (maxDate && d.isAfter(maxDate)) d = maxDate;
    if (!isControlled) setInternal(d);
    onChange === null || onChange === void 0 || onChange(d);
  }, [isControlled, onChange, minDate, maxDate]);
  var prevDisabled = navigationLocked || (minDate ? value.isSame(minDate, 'day') : false);
  var nextDisabled = navigationLocked || (maxDate ? value.isSame(maxDate, 'day') : false);
  var _useState3 = useState(false),
    _useState4 = _slicedToArray(_useState3, 2),
    calendarOpen = _useState4[0],
    setCalendarOpen = _useState4[1];
  var centerLabel = useMemo(function () {
    var djLoc = toDayjsLocale(engineLocale);
    var dl = value.locale(djLoc);
    var isToday = value.isSame(today, 'day');
    var todayText = locales.getText(I18N_TODAY);
    var dateStr = djLoc === 'en' ? dl.format('MMM D') : dl.format('M月D日');
    var weekStr = dl.format('ddd');
    var parts = [];
    if (isToday && todayText) parts.push(todayText);
    parts.push(dateStr);
    parts.push(weekStr);
    return parts.join(djLoc === 'en' ? ' · ' : ' · ');
  }, [value, today, engineLocale]);
  return /*#__PURE__*/React.createElement("div", {
    className: classNames('pisell-reservation-schedule', className),
    style: style
  }, /*#__PURE__*/React.createElement("div", {
    className: "pisell-reservation-schedule-inner"
  }, /*#__PURE__*/React.createElement("button", {
    type: "button",
    className: "pisell-reservation-schedule-btn",
    "aria-label": locales.getText(I18N_PREV),
    disabled: prevDisabled,
    onClick: function onClick() {
      if (navigationLocked) return;
      emit(value.subtract(1, 'day'));
    }
  }, /*#__PURE__*/React.createElement(ChevronLeft, {
    className: "pisell-reservation-schedule-btn-icon"
  })), navigationLocked ? /*#__PURE__*/React.createElement("span", {
    className: "pisell-reservation-schedule-label pisell-reservation-schedule-label--static",
    "aria-label": centerLabel
  }, centerLabel) : /*#__PURE__*/React.createElement("button", {
    type: "button",
    className: "pisell-reservation-schedule-label pisell-reservation-schedule-label-trigger",
    "aria-label": locales.getText(I18N_PICK_DATE),
    "aria-haspopup": "dialog",
    "aria-expanded": calendarOpen,
    onClick: function onClick() {
      return setCalendarOpen(true);
    }
  }, centerLabel), /*#__PURE__*/React.createElement("button", {
    type: "button",
    className: "pisell-reservation-schedule-btn",
    "aria-label": locales.getText(I18N_NEXT),
    disabled: nextDisabled,
    onClick: function onClick() {
      if (navigationLocked) return;
      emit(value.add(1, 'day'));
    }
  }, /*#__PURE__*/React.createElement(ChevronRight, {
    className: "pisell-reservation-schedule-btn-icon"
  }))), /*#__PURE__*/React.createElement(Modal, {
    title: locales.getText(I18N_PICK_DATE_TITLE),
    open: !navigationLocked && calendarOpen,
    onCancel: function onCancel() {
      return setCalendarOpen(false);
    },
    footer: null,
    width: "auto",
    centered: true,
    destroyOnClose: true,
    wrapClassName: "pisell-reservation-schedule-date-modal"
  }, /*#__PURE__*/React.createElement(DateCalendar, {
    value: value,
    minDate: minDate,
    maxDate: maxDate,
    onChange: function onChange(d) {
      if (d) {
        emit(dayjs(d));
        setCalendarOpen(false);
      }
    }
  })));
}
PisellReservationSchedule.displayName = 'PisellReservationSchedule';