function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
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, { useState, useCallback } from 'react';
import dayjs from 'dayjs';
import classNames from 'classnames';
import { Spin } from 'antd';
import { PisellReservationSchedule } from "../pisellReservationSchedule";
import { PisellTimeNavigator } from "../pisellTimeNavigator";
import { formatScheduleAtLabel } from "./utils/formatScheduleAtLabel";
import "./PisellReservationScheduleBand.less";
function startOfDay(d) {
  return d.startOf('day');
}
function withTimeOnDate(dateDay, timeSource) {
  return startOfDay(dateDay).hour(timeSource.hour()).minute(timeSource.minute()).second(0).millisecond(0);
}

/**
 * 预约顶栏组合：营业日切换 + 锚定到选中日的时间导航轴
 */
export function PisellReservationScheduleBand(props) {
  var valueProp = props.value,
    onChange = props.onChange,
    defaultValue = props.defaultValue,
    scheduleProps = props.scheduleProps,
    timeNavigatorProps = props.timeNavigatorProps,
    scheduleStartSlot = props.scheduleStartSlot,
    scheduleEndSlot = props.scheduleEndSlot,
    scheduleAfterDateSlot = props.scheduleAfterDateSlot,
    _props$dataLoading = props.dataLoading,
    dataLoading = _props$dataLoading === void 0 ? false : _props$dataLoading,
    dataLoadingTip = props.dataLoadingTip,
    _props$hideTimeNaviga = props.hideTimeNavigator,
    hideTimeNavigator = _props$hideTimeNaviga === void 0 ? false : _props$hideTimeNaviga,
    className = props.className,
    style = props.style,
    scheduleRowClassName = props.scheduleRowClassName;
  var isControlled = valueProp !== undefined;
  var _useState = useState(function () {
      var _defaultValue$date, _defaultValue$at;
      var d = (_defaultValue$date = defaultValue === null || defaultValue === void 0 ? void 0 : defaultValue.date) !== null && _defaultValue$date !== void 0 ? _defaultValue$date : dayjs().startOf('day');
      var atSrc = (_defaultValue$at = defaultValue === null || defaultValue === void 0 ? void 0 : defaultValue.at) !== null && _defaultValue$at !== void 0 ? _defaultValue$at : dayjs();
      return {
        date: startOfDay(dayjs(d)),
        at: withTimeOnDate(dayjs(d), dayjs(atSrc))
      };
    }),
    _useState2 = _slicedToArray(_useState, 2),
    internal = _useState2[0],
    setInternal = _useState2[1];
  var effective = isControlled ? valueProp : internal;
  var emit = useCallback(function (next) {
    /** at 保留时间轴返回的绝对时刻（跨日营业时可能为锚定日的次日） */
    var normalized = {
      date: startOfDay(next.date),
      at: next.at
    };
    if (!isControlled) setInternal(normalized);
    onChange === null || onChange === void 0 || onChange(normalized);
  }, [isControlled, onChange]);
  var handleDateChange = useCallback(function (d) {
    emit({
      date: startOfDay(d),
      at: withTimeOnDate(d, effective.at)
    });
  }, [emit, effective.at]);
  var handleTimeChange = useCallback(function (t) {
    /** 顶栏 date 保持营业日锚定日；跨自然日时刻由 formatScheduleAtLabel 展示为「MM-DD HH:mm」 */
    emit({
      date: startOfDay(effective.date),
      at: t
    });
  }, [emit, effective.date]);
  var formatTimeLabel = useCallback(function (t) {
    return formatScheduleAtLabel(t, effective.date);
  }, [effective.date]);
  return /*#__PURE__*/React.createElement("div", {
    className: classNames('pisell-reservation-schedule-band', dataLoading && 'pisell-reservation-schedule-band--data-loading', className),
    style: style
  }, dataLoading ? /*#__PURE__*/React.createElement("div", {
    className: "pisell-reservation-schedule-band-data-loading-overlay",
    "aria-busy": "true",
    "aria-live": "polite"
  }, /*#__PURE__*/React.createElement(Spin, {
    size: "small",
    tip: dataLoadingTip != null ? dataLoadingTip : undefined
  })) : null, /*#__PURE__*/React.createElement("div", {
    className: classNames('pisell-reservation-schedule-band-schedule-row', scheduleRowClassName)
  }, /*#__PURE__*/React.createElement("div", {
    className: "pisell-reservation-schedule-band-slot-start"
  }, scheduleStartSlot), /*#__PURE__*/React.createElement("div", {
    className: "pisell-reservation-schedule-band-schedule"
  }, /*#__PURE__*/React.createElement(PisellReservationSchedule, _extends({
    value: effective.date,
    onChange: handleDateChange
  }, scheduleProps)), scheduleAfterDateSlot ? /*#__PURE__*/React.createElement("div", {
    className: "pisell-reservation-schedule-band-after-date"
  }, scheduleAfterDateSlot) : null), /*#__PURE__*/React.createElement("div", {
    className: "pisell-reservation-schedule-band-slot-end"
  }, scheduleEndSlot)), hideTimeNavigator ? null : /*#__PURE__*/React.createElement("div", {
    className: "pisell-reservation-schedule-band-navigator"
  }, /*#__PURE__*/React.createElement(PisellTimeNavigator, _extends({
    referenceDate: effective.date,
    value: effective.at
  }, timeNavigatorProps, {
    /**
     * onChange / formatTimeLabel 写在 spread 之后，避免 timeNavigatorProps 覆盖。
     */
    onChange: handleTimeChange,
    formatTimeLabel: formatTimeLabel
  }))));
}
PisellReservationScheduleBand.displayName = 'PisellReservationScheduleBand';