import { locales } from '@pisell/utils';
import dayjs from 'dayjs';
function toOptionalBoolean(value) {
  if (value === true || value === 1 || value === '1') return true;
  if (value === false || value === 0 || value === '0') return false;
  return undefined;
}
function toOptionalNumber(value) {
  var next = Number(value);
  return Number.isFinite(next) ? next : undefined;
}

/**
 * 解析 CartItems 的卡片展示配置；bookingConfig 未下发对应字段时保持当前默认展示。
 */
export function resolveSaleDetailCartItemCardStyle(bookingConfig, kind) {
  var _bookingConfig$config, _toOptionalBoolean, _toOptionalBoolean2;
  var cartSettings = (bookingConfig === null || bookingConfig === void 0 || (_bookingConfig$config = bookingConfig.config) === null || _bookingConfig$config === void 0 ? void 0 : _bookingConfig$config.cart_settings_tab) || {};
  var styleConfig = cartSettings["".concat(kind, "_product_card_style")] || {};
  return {
    isShowImage: (_toOptionalBoolean = toOptionalBoolean(styleConfig.show_product_image)) !== null && _toOptionalBoolean !== void 0 ? _toOptionalBoolean : true,
    isShowDelete: (_toOptionalBoolean2 = toOptionalBoolean(styleConfig.show_remove_button)) !== null && _toOptionalBoolean2 !== void 0 ? _toOptionalBoolean2 : true
  };
}
export function resolveSaleDetailShowContactInfo(bookingConfig) {
  var _bookingConfig$config2, _cartSettings$display;
  var cartSettings = (bookingConfig === null || bookingConfig === void 0 || (_bookingConfig$config2 = bookingConfig.config) === null || _bookingConfig$config2 === void 0 ? void 0 : _bookingConfig$config2.cart_settings_tab) || {};
  return Boolean((_cartSettings$display = cartSettings.display_contact_info) === null || _cartSettings$display === void 0 ? void 0 : _cartSettings$display.show_contact_info);
}
export function resolveSaleDetailTransactionHabit(bookingConfig) {
  var _bookingConfig$config3;
  var raw = (bookingConfig === null || bookingConfig === void 0 || (_bookingConfig$config3 = bookingConfig.config) === null || _bookingConfig$config3 === void 0 || (_bookingConfig$config3 = _bookingConfig$config3.information_tab) === null || _bookingConfig$config3 === void 0 ? void 0 : _bookingConfig$config3.transaction_habit_selection) || {};
  var rawType = String(raw.type || '').trim();
  var type = rawType === 'pay_now_service_later' || rawType === 'service_now_pay_later' ? rawType : undefined;
  return {
    type: type,
    buttonRows: toOptionalNumber(raw.button_rows)
  };
}
export function getLocalizedDateFormat() {
  var _locales$getLocale;
  var locale = ((_locales$getLocale = locales.getLocale) === null || _locales$getLocale === void 0 ? void 0 : _locales$getLocale.call(locales)) || 'en';
  var isChineseLocale = String(locale).toLowerCase().startsWith('zh');
  return isChineseLocale ? 'YYYY/MM/DD' : 'DD/MM/YYYY';
}
export function getLocalizedDateTimeFormat() {
  return "".concat(getLocalizedDateFormat(), " HH:mm");
}
export function formatLocalizedDateTime(value) {
  var emptyText = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '-';
  if (value === undefined || value === null || value === '') return emptyText;
  var date = dayjs(value);
  return date.isValid() ? date.format(getLocalizedDateTimeFormat()) : String(value);
}

/**
 * 统一处理扫码失败的多语言提示文案。
 *
 * @param val 扫码接口返回结果
 * @param scene 使用场景，决定默认兜底文案
 * @returns 当前语言下的失败提示文案
 */
export function showDiscountScanFailMessage(val) {
  var scene = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'goodpass';
  var reasonKey = val === null || val === void 0 ? void 0 : val.unavailableReasonKey;
  var reasonMap = {
    total_credits: locales.getText('pisell2.text.goodpass.limit.total_credits'),
    per_user_limit: locales.getText('pisell2.text.goodpass.limit.per_user_limit'),
    max_per_day: locales.getText('pisell2.text.goodpass.limit.max_per_day'),
    max_per_week: locales.getText('pisell2.text.goodpass.limit.max_per_week'),
    max_per_month: locales.getText('pisell2.text.goodpass.limit.max_per_month')
  };
  if (reasonKey && reasonMap[reasonKey]) {
    return reasonMap[reasonKey];
  }
  if ((val === null || val === void 0 ? void 0 : val.type) === 'server') {
    return scene === 'goodpass' ? locales.getText('pisell2.text.goodpass.code-invalid') : locales.getText('pisell2.voucher.code-invalid');
  }
  var unavailableReasonMap = {
    time_limit: locales.getText('pisell2.text.goodpass.code-invalid-expired')
  };
  if (val !== null && val !== void 0 && val.unavailableReason && unavailableReasonMap[val.unavailableReason]) {
    return unavailableReasonMap[val.unavailableReason];
  }
  return scene === 'goodpass' ? locales.getText('pisell2.text.goodpass.code-invalid-cart') : locales.getText('pisell2.voucher.code-invalid-cart');
}