function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
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 { locales } from '@pisell/utils';
/**
 * 宿主 action / NativePage 用的订单主键：优先 order.id，否则顶层 order_id。
 */
export function resolveOrderIdForHostActions(bookingRaw) {
  var _ref, _order$id;
  var order = bookingRaw.order;
  var id = (_ref = (_order$id = order === null || order === void 0 ? void 0 : order.id) !== null && _order$id !== void 0 ? _order$id : order === null || order === void 0 ? void 0 : order.order_id) !== null && _ref !== void 0 ? _ref : bookingRaw.order_id;
  if (id == null || id === '') return undefined;
  if (typeof id === 'number' && Number.isFinite(id)) return id;
  var s = String(id).trim();
  if (!s) return undefined;
  var n = Number(s);
  return Number.isFinite(n) && String(n) === s ? n : s;
}
export function resolveBookingIdForHostActions(bookingRaw) {
  var id = bookingRaw.id;
  if (id == null || id === '') return undefined;
  if (typeof id === 'number' && Number.isFinite(id)) return id;
  var s = String(id).trim();
  if (!s) return undefined;
  var n = Number(s);
  return Number.isFinite(n) && String(n) === s ? n : s;
}
export function resolveBookingDateTime(bookingRaw, atKey, dateKey, timeKey) {
  var at = bookingRaw[atKey];
  if (at != null && String(at).trim() !== '') return String(at).trim();
  var date = bookingRaw[dateKey];
  var time = bookingRaw[timeKey];
  if (date == null || time == null) return undefined;
  var dateText = String(date).trim();
  var timeText = String(time).trim();
  if (!dateText || !timeText) return undefined;
  return "".concat(dateText, " ").concat(timeText);
}
export function resolveResourceIdFromRow(row) {
  if (!(row !== null && row !== void 0 && row.id)) return undefined;
  var id = String(row.id).trim();
  return id || undefined;
}
export function buildEditBookingResourceSnapshot(selected) {
  var _selected$resource, _selected$resource$id, _selected$resource2, _selected$resource$fo, _selected$resource3, _selected$resource$co, _selected$resource4, _selected$resource$ma, _selected$resource5;
  var raw = (_selected$resource = selected.resource) !== null && _selected$resource !== void 0 && _selected$resource.raw && _typeof(selected.resource.raw) === 'object' ? selected.resource.raw : selected.row;
  var id = (_selected$resource$id = (_selected$resource2 = selected.resource) === null || _selected$resource2 === void 0 ? void 0 : _selected$resource2.id) !== null && _selected$resource$id !== void 0 ? _selected$resource$id : raw === null || raw === void 0 ? void 0 : raw.id;
  if (id == null || String(id).trim() === '') return undefined;
  return _objectSpread(_objectSpread({}, raw), {}, {
    id: id,
    form_id: (_selected$resource$fo = (_selected$resource3 = selected.resource) === null || _selected$resource3 === void 0 ? void 0 : _selected$resource3.form_id) !== null && _selected$resource$fo !== void 0 ? _selected$resource$fo : raw === null || raw === void 0 ? void 0 : raw.form_id,
    code: (_selected$resource$co = (_selected$resource4 = selected.resource) === null || _selected$resource4 === void 0 ? void 0 : _selected$resource4.code) !== null && _selected$resource$co !== void 0 ? _selected$resource$co : raw === null || raw === void 0 ? void 0 : raw.code,
    main_field: (_selected$resource$ma = (_selected$resource5 = selected.resource) === null || _selected$resource5 === void 0 ? void 0 : _selected$resource5.main_field) !== null && _selected$resource$ma !== void 0 ? _selected$resource$ma : raw === null || raw === void 0 ? void 0 : raw.main_field
  });
}
function getResourceFormId(resource) {
  if ((resource === null || resource === void 0 ? void 0 : resource.form_id) == null) return undefined;
  var formId = String(resource.form_id).trim();
  return formId || undefined;
}
export function isSameResourceType(source, target) {
  var sourceFormId = getResourceFormId(source);
  var targetFormId = getResourceFormId(target);
  if (!sourceFormId || !targetFormId) return true;
  return sourceFormId === targetFormId;
}
export function getSalesHostModule(pisellos) {
  var _getModule, _ref2;
  return pisellos === null || pisellos === void 0 || (_getModule = (_ref2 = pisellos).getModule) === null || _getModule === void 0 ? void 0 : _getModule.call(_ref2, 'sales');
}
export function updateLocalOrder(action, orderId) {
  if (orderId == null || typeof action !== 'function') return;
  action({
    type: 'webpos.updateLocalOrderApi',
    data: {
      order_id: orderId
    }
  });
}

/** 预约是否已结束（`status` / `appointment_status` 为 completed）。 */
export function isCompletedBookingRaw(bookingRaw) {
  var _bookingRaw$status, _bookingRaw$appointme;
  var s = String((_bookingRaw$status = bookingRaw.status) !== null && _bookingRaw$status !== void 0 ? _bookingRaw$status : '').trim().toLowerCase();
  var a = String((_bookingRaw$appointme = bookingRaw.appointment_status) !== null && _bookingRaw$appointme !== void 0 ? _bookingRaw$appointme : '').trim().toLowerCase();
  return s === 'completed' || a === 'completed';
}
export function parseBookingProducts(bookingRaw) {
  var p = bookingRaw.products;
  if (!Array.isArray(p)) return [];
  return p.filter(function (x) {
    return x && _typeof(x) === 'object';
  });
}
export function orderCurrencySymbol(order) {
  if (!order) return '$';
  var s = order.currency_symbol;
  if (s != null && String(s).trim() !== '') return String(s).trim();
  return '$';
}
export function resolveAppointmentStatusTag(bookingRaw) {
  var _bookingRaw$appointme2, _statusMap$rawStatus;
  var rawStatus = String((_bookingRaw$appointme2 = bookingRaw === null || bookingRaw === void 0 ? void 0 : bookingRaw.appointment_status) !== null && _bookingRaw$appointme2 !== void 0 ? _bookingRaw$appointme2 : '').trim();
  var statusMap = {
    new: {
      label: locales.getText('pisell2.text.status.new'),
      tone: 'new'
    },
    confirmed: {
      label: locales.getText('pisell2.text.status.confirmed'),
      tone: 'confirmed'
    },
    arrived: {
      label: locales.getText('pisell2.text.status.arrived'),
      tone: 'arrived'
    },
    started: {
      label: locales.getText('pisell2.text.status.started'),
      tone: 'started'
    },
    cancelled: {
      label: locales.getText('pisell2.text.status.cancelled'),
      tone: 'cancelled'
    },
    no_show: {
      label: locales.getText('pisell2.text.status.no_show'),
      tone: 'no-show'
    },
    completed: {
      label: locales.getText('pisell2.text.status.completed'),
      tone: 'completed'
    }
  };
  return (_statusMap$rawStatus = statusMap[rawStatus]) !== null && _statusMap$rawStatus !== void 0 ? _statusMap$rawStatus : statusMap.new;
}