function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
import dayjs from 'dayjs';
import { RESERVATION_TIMELINE_BUSINESS_RANGE } from "../reservationConstants";

/**
 * 将 `operating_day_boundary` 规范为「单个 `{ type, time }`」再交给
 * {@link boundaryClockFromOperatingDayBoundary}：若为数组则优先 `start_time`，否则 `end_time`，再否则取首项。
 */
function normalizeOperatingDayBoundaryRaw(raw) {
  var _ref, _pick;
  if (raw == null) return undefined;
  if (!Array.isArray(raw)) return raw;
  var pick = function pick(t) {
    return raw.find(function (x) {
      return x != null && _typeof(x) === 'object' && String(x.type) === t;
    });
  };
  return (_ref = (_pick = pick('start_time')) !== null && _pick !== void 0 ? _pick : pick('end_time')) !== null && _ref !== void 0 ? _ref : raw[0];
}

/**
 * 从 `store.getDataByModel('core', 'core')` 的返回值上取出营业日界钟配置。
 * 兼容两种挂载：嵌在 `.core.operating_day_boundary`，或与模型根同级的 `.operating_day_boundary`
 *（部分宿主/历史数据不会多包一层 `core`）。
 */
export function resolveOperatingDayBoundaryFromCoreModel(coreModel) {
  if (coreModel == null || _typeof(coreModel) !== 'object') {
    return undefined;
  }
  var root = coreModel;
  var raw;
  if (root.core != null && _typeof(root.core) === 'object') {
    raw = root.core.operating_day_boundary;
  }
  if (raw == null) {
    raw = root.operating_day_boundary;
  }
  return normalizeOperatingDayBoundaryRaw(raw);
}
export function normalizeBoundaryClock(raw) {
  var _parts$, _parts$2;
  var t = raw.trim();
  if (!t) return null;
  var parts = t.split(':').map(function (p) {
    return p.trim();
  });
  var h = Number(parts[0]);
  var m = Number((_parts$ = parts[1]) !== null && _parts$ !== void 0 ? _parts$ : '0');
  var s = Number((_parts$2 = parts[2]) !== null && _parts$2 !== void 0 ? _parts$2 : '0');
  if (!Number.isFinite(h) || !Number.isFinite(m) || !Number.isFinite(s)) return null;
  if (h < 0 || h > 23 || m < 0 || m > 59 || s < 0 || s > 59) return null;
  return "".concat(String(h).padStart(2, '0'), ":").concat(String(m).padStart(2, '0'));
}

/**
 * 解析引擎 `core.operating_day_boundary`：`start_time` / `end_time` 均读取 `time` 作为营业日跨日界钟；
 * 无法解析或类型不匹配时返回 `null`（调用方退回 {@link RESERVATION_TIMELINE_BUSINESS_RANGE}）。
 */
export function boundaryClockFromOperatingDayBoundary(operatingDayBoundary) {
  var _b$time;
  var b = operatingDayBoundary;
  var type = b === null || b === void 0 ? void 0 : b.type;
  if (type !== 'start_time' && type !== 'end_time') return null;
  return normalizeBoundaryClock(String((_b$time = b === null || b === void 0 ? void 0 : b.time) !== null && _b$time !== void 0 ? _b$time : ''));
}

/**
 * `operating_day_boundary.type === 'end_time'` 时：顶栏选中日 D 表示「营业日在 D 的界钟结束」，
 * 营业窗为 [D−1 界钟, D 界钟)；`getRangeBounds` / 列表 `booking_time_start_between` 须把锚定自然日设为 D−1。
 * `start_time`（或其它）：锚定日为 D（界钟在 D 起算至次日同钟）。
 */
export function timelineReferenceDateForScheduleDay(scheduleDate, operatingDayBoundary) {
  var _b$type;
  var d = dayjs(scheduleDate).startOf('day');
  var b = operatingDayBoundary;
  if (String((_b$type = b === null || b === void 0 ? void 0 : b.type) !== null && _b$type !== void 0 ? _b$type : '') === 'end_time') {
    return d.subtract(1, 'day');
  }
  return d;
}

/**
 * 与引擎 `core` 中 `operating_day_boundary` 对齐，生成 PisellTimeNavigator / 日历共用的
 * {@link TimeRangeOption}（起止钟面相同，由 getRangeBounds 展开为 24h）。
 * 否则退回 {@link RESERVATION_TIMELINE_BUSINESS_RANGE}（默认 02:00–次日 02:00）。
 */
export function timelineRangeFromOperatingDayBoundary(operatingDayBoundary) {
  var normalized = boundaryClockFromOperatingDayBoundary(operatingDayBoundary);
  if (!normalized) {
    return {
      start: RESERVATION_TIMELINE_BUSINESS_RANGE.start,
      end: RESERVATION_TIMELINE_BUSINESS_RANGE.end
    };
  }
  return {
    start: normalized,
    end: normalized
  };
}