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); }
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 _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
import { useCallback, useEffect, useMemo, useState } from 'react';
import { useRequest } from 'ahooks';
import dayjs from 'dayjs';
import { defaultPaymentStatusKeys } from "../config/booking";
import { getOrderList, getBookingList } from "../../pisellSalesGrid/serve";
function getDefaultSearchParams(perspective) {
  if (perspective === 'monitor') {
    return {
      filter: {
        values: {
          order_payment_status: defaultPaymentStatusKeys,
          booking_time_start_between: [dayjs().startOf('day'), dayjs().endOf('day')]
        }
      }
    };
  }
  return {
    filter: {
      values: {
        payment_status: defaultPaymentStatusKeys,
        orderDate: [dayjs(), dayjs()]
      }
    },
    sort: 'created_at:desc'
  };
}

/**
 * Sales Management 内使用的列表数据 hook：统一发起订单/预订列表请求，
 * 管理分页、筛选、多选状态，并返回传给 PisellSalesGrid 的受控 props，
 * 使 Sales Grid 成为通用展示组件。
 */
export function useSalesGridData(params) {
  var _getBookingList$data$, _getBookingList$data, _getOrderList$data$li, _getOrderList$data, _getBookingList$data$2, _getBookingList$data2, _getOrderList$data$co, _getOrderList$data2;
  var perspective = params.perspective,
    extParams = params.extParams;
  var _useState = useState({
      pageNumber: 1,
      pageSize: 50
    }),
    _useState2 = _slicedToArray(_useState, 2),
    pagination = _useState2[0],
    setPagination = _useState2[1];
  var _useState3 = useState(function () {
      return getDefaultSearchParams(perspective);
    }),
    _useState4 = _slicedToArray(_useState3, 2),
    searchParams = _useState4[0],
    setSearchParams = _useState4[1];
  var _useState5 = useState([]),
    _useState6 = _slicedToArray(_useState5, 2),
    selectedKeys = _useState6[0],
    setSelectedKeys = _useState6[1];
  var _useState7 = useState([]),
    _useState8 = _slicedToArray(_useState7, 2),
    selectedRows = _useState8[0],
    setSelectedRows = _useState8[1];
  var requestParams = useMemo(function () {
    var keyword = searchParams.keyword,
      filter = searchParams.filter,
      sort = searchParams.sort;
    var filterVals = (filter === null || filter === void 0 ? void 0 : filter.values) || {};
    return _objectSpread(_objectSpread({
      keyword: keyword,
      sort: sort
    }, filterVals), {}, {
      skip: pagination.pageNumber,
      num: pagination.pageSize
    });
  }, [searchParams, pagination.pageNumber, pagination.pageSize]);
  var _getOrderList = useRequest(getOrderList, {
    manual: true
  });
  var _getBookingList = useRequest(getBookingList, {
    manual: true
  });
  var runListParams = useMemo(function () {
    return _objectSpread(_objectSpread({}, requestParams), (extParams === null || extParams === void 0 ? void 0 : extParams.list) || {});
  }, [requestParams, extParams === null || extParams === void 0 ? void 0 : extParams.list]);
  useEffect(function () {
    if (perspective === 'monitor') {
      _getBookingList.run(runListParams);
    } else {
      _getOrderList.run(runListParams);
    }
  }, [perspective, runListParams]);

  // 切换视角时重置筛选与分页
  useEffect(function () {
    setSearchParams(getDefaultSearchParams(perspective));
    setPagination(function (p) {
      return _objectSpread(_objectSpread({}, p), {}, {
        pageNumber: 1
      });
    });
    setSelectedKeys([]);
    setSelectedRows([]);
  }, [perspective]);
  var onPageChange = useCallback(function (pageNumber, pageSize) {
    if (pageNumber < 1 || pageSize < 1) return;
    setPagination({
      pageNumber: pageNumber,
      pageSize: pageSize
    });
  }, []);
  var onSearch = useCallback(function (params) {
    setSearchParams(params || {});
    setPagination(function (prev) {
      return _objectSpread(_objectSpread({}, prev), {}, {
        pageNumber: 1
      });
    });
  }, []);
  var onReset = useCallback(function () {
    setSearchParams(getDefaultSearchParams(perspective));
    setPagination(function (prev) {
      return _objectSpread(_objectSpread({}, prev), {}, {
        pageNumber: 1
      });
    });
  }, [perspective]);
  var onSelectionChange = useCallback(function (keys, rows) {
    setSelectedKeys(keys);
    setSelectedRows(rows);
  }, []);
  var onRefresh = useCallback(function () {
    if (perspective === 'monitor') {
      _getBookingList.run(runListParams);
    } else {
      _getOrderList.run(runListParams);
    }
  }, [perspective, runListParams, _getBookingList, _getOrderList]);
  var refreshAsync = useCallback(function () {
    if (perspective === 'monitor') {
      return _getBookingList.runAsync(runListParams);
    }
    return _getOrderList.runAsync(runListParams);
  }, [perspective, runListParams, _getBookingList, _getOrderList]);
  var data = perspective === 'monitor' ? (_getBookingList$data$ = (_getBookingList$data = _getBookingList.data) === null || _getBookingList$data === void 0 ? void 0 : _getBookingList$data.list) !== null && _getBookingList$data$ !== void 0 ? _getBookingList$data$ : [] : (_getOrderList$data$li = (_getOrderList$data = _getOrderList.data) === null || _getOrderList$data === void 0 ? void 0 : _getOrderList$data.list) !== null && _getOrderList$data$li !== void 0 ? _getOrderList$data$li : [];
  var loading = perspective === 'monitor' ? _getBookingList.loading : _getOrderList.loading;
  var total = perspective === 'monitor' ? (_getBookingList$data$2 = (_getBookingList$data2 = _getBookingList.data) === null || _getBookingList$data2 === void 0 ? void 0 : _getBookingList$data2.count) !== null && _getBookingList$data$2 !== void 0 ? _getBookingList$data$2 : 0 : (_getOrderList$data$co = (_getOrderList$data2 = _getOrderList.data) === null || _getOrderList$data2 === void 0 ? void 0 : _getOrderList$data2.count) !== null && _getOrderList$data$co !== void 0 ? _getOrderList$data$co : 0;
  return {
    data: data,
    loading: loading,
    total: total,
    pagination: pagination,
    onPageChange: onPageChange,
    searchParams: searchParams,
    onSearch: onSearch,
    onReset: onReset,
    selectedKeys: selectedKeys,
    selectedRows: selectedRows,
    onSelectionChange: onSelectionChange,
    rowKey: 'id',
    onRefresh: onRefresh,
    refreshAsync: refreshAsync
  };
}