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 { getDateIsInSchedule } from "../Schedule/getDateIsInSchedule";
function toBoolean(value) {
  if (typeof value === 'boolean') return value;
  if (typeof value === 'string') return value === 'true' || value === '1';
  if (typeof value === 'number') return value !== 0;
  return false;
}
function resolveMessage() {
  for (var _len = arguments.length, candidates = new Array(_len), _key = 0; _key < _len; _key++) {
    candidates[_key] = arguments[_key];
  }
  for (var _i = 0, _candidates = candidates; _i < _candidates.length; _i++) {
    var val = _candidates[_i];
    if (!val) continue;
    if (typeof val === 'string' && val.length > 0) return val;
    if (_typeof(val) === 'object' && val !== null && !Array.isArray(val)) return val;
  }
  return null;
}
export function computeAvailability(params) {
  var config = params.config,
    scheduleList = params.scheduleList;
  if (!config) {
    return {
      available: true,
      reason: null,
      message: null
    };
  }
  if (config['basic.enable'] === false) {
    return {
      available: false,
      reason: 'shop_disabled',
      message: resolveMessage(config['basic.unavailable_message'], config['availability.closed_message'])
    };
  }
  if (toBoolean(config['availability.paused'])) {
    return {
      available: false,
      reason: 'paused',
      message: resolveMessage(config['availability.pause_message'], config['availability.closed_message'])
    };
  }
  var operatingHourIds = config['availability.operating_hours'];
  if (Array.isArray(operatingHourIds) && operatingHourIds.length > 0 && scheduleList !== null && scheduleList !== void 0 && scheduleList.length) {
    var relevantSchedules = scheduleList.filter(function (s) {
      return operatingHourIds.includes(s.id);
    });
    if (relevantSchedules.length > 0) {
      var now = dayjs().format('YYYY-MM-DD HH:mm:ss');
      var isOpen = getDateIsInSchedule(now, relevantSchedules);
      if (!isOpen) {
        return {
          available: false,
          reason: 'closed',
          message: resolveMessage(config['availability.closed_message'])
        };
      }
    }
  }
  if (config['reservation.enable'] === false) {
    return {
      available: false,
      reason: 'reservation_disabled',
      message: resolveMessage(config['availability.closed_message'])
    };
  }
  return {
    available: true,
    reason: null,
    message: null
  };
}