import dayjs from 'dayjs';
import { getDurationValue } from "./flexible";
/**
 * 把一天切成等长时间槽，输出 timeSlices（开始时间标签）与 durationSlicesBasedOnTime（基于切片的时长选项）。
 *
 * 与 info2 `sliceDayIntoFiveMinutes` 行为等价；为了不依赖 `locales`，label 输出原始时间字符串（'08:00 AM' / '90'），
 * UI 侧需要本地化展示时可基于 value 自行包装。
 *
 * @param config booking config `basic.service_time`，{ value: minutes }
 * @param isDay 是否跨日预约（true 时按 1~30 天切片）
 */
export function sliceDayIntoFiveMinutes(config, isDay) {
  var startTime = dayjs().startOf('day');
  var endTime = dayjs().endOf('day');
  var timeSlice = config && config.value || 5;
  var timeSlices = [];
  var durationSlicesBasedOnTime = [];
  var currentTime = startTime;
  if (isDay) {
    var currentDay = 1;
    while (currentDay <= 30) {
      timeSlices.push({
        label: currentTime.format('hh:mm A'),
        value: currentTime.format('HH:mm')
      });
      durationSlicesBasedOnTime.push({
        label: String(currentDay),
        value: currentDay
      });
      currentDay += 1;
    }
    return {
      timeSlices: timeSlices,
      durationSlicesBasedOnTime: durationSlicesBasedOnTime
    };
  }
  while (currentTime.isBefore(endTime)) {
    timeSlices.push({
      label: currentTime.format('hh:mm A'),
      value: currentTime.format('HH:mm')
    });
    var nextTime = currentTime.add(timeSlice, 'minute');
    var durationValue = nextTime.diff(startTime, 'minute');
    durationSlicesBasedOnTime.push({
      label: String(durationValue),
      value: durationValue
    });
    currentTime = nextTime;
  }
  return {
    timeSlices: timeSlices,
    durationSlicesBasedOnTime: durationSlicesBasedOnTime
  };
}

/**
 * 取「下一个 duration 选项」：优先用 defaultValue；否则在 array 中找匹配的；找不到取第一个。
 * 与 info2 `findNextDuration` 等价。
 */
export function findNextDuration(current, array, defaultValue) {
  if (defaultValue !== undefined && defaultValue !== null) return defaultValue;
  if (!array || array.length === 0) return undefined;
  for (var i = 0; i < array.length; i += 1) {
    if (array[i].value === (current === null || current === void 0 ? void 0 : current.value)) return array[i].value;
  }
  return array[0].value;
}

/** 取首选 time slice：若指定 date（HH:mm）则找第一个 >= date 的；否则第一个。 */
export function findNextSlice(params) {
  var _timeSlices$index;
  var timeSlices = params.timeSlices,
    _params$index = params.index,
    index = _params$index === void 0 ? 0 : _params$index,
    _params$date = params.date,
    date = _params$date === void 0 ? '' : _params$date;
  var currentTimeValue = date || dayjs().format('HH:mm');
  for (var i = 0; i < timeSlices.length; i += 1) {
    if (timeSlices[i].value !== 'now' && timeSlices[i].value >= currentTimeValue) {
      return timeSlices[i].value;
    }
  }
  return (_timeSlices$index = timeSlices[index]) === null || _timeSlices$index === void 0 ? void 0 : _timeSlices$index.value;
}

/** 取 endDate；duration='flexible' 时取 endDate 与店铺营业时间的较小值，否则 startDate + duration 分钟。 */
export function getEndDate(startDate, duration, endDate, shopOpeningHours) {
  if (duration === 'flexible') {
    var openingDate = dayjs("".concat(startDate.format('YYYY-MM-DD'), " ").concat(shopOpeningHours || '23:59'));
    if (dayjs(startDate).isAfter(openingDate)) {
      openingDate = openingDate.add(1, 'day');
    }
    if (!endDate) return openingDate;
    var endDateObj = dayjs(endDate);
    if (endDateObj.isAfter(openingDate)) return openingDate;
    return endDateObj;
  }
  return startDate.add(Number(duration), 'minute');
}
export { getDurationValue };