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); }
import React, { useEffect, useMemo } from 'react';
import { useRequest } from 'ahooks';
import { locales } from '@pisell/utils';
import { PisellConfigProvider } from '@pisell/materials';
import { LocalizationProvider } from '@pisell/date-picker';
import { AdapterDayjs } from '@pisell/date-picker/es/AdapterDayjs';
import { request, setOsQueryWebPos } from "../../utils";
import useEngineContext from "../../hooks/useEngineContext";
import PisellSalesGrid from "../pisellSalesGrid";
import usePerspectives from "./hooks/usePerspectives";
import { useSalesGridData } from "./hooks/useSalesGridData";
import salesManagementLocales from "./locales";
import { getRelationTables } from "./serve";
var PisellSalesManagement = function PisellSalesManagement(props) {
  var _context$appHelper, _utils$getApp, _context$engine, _context$engine2, _app$history, _app$history$useParam, _context$engine3, _context$engine4, _context$engine5, _context$engine6, _context$engine7;
  var perspective = props.perspective,
    __id = props.__id,
    keyword = props.keyword,
    mode = props.mode,
    hideToolbarSearch = props.hideToolbarSearch,
    onOpenDetail = props.onOpenDetail;
  var context = useEngineContext();
  var utils = (context === null || context === void 0 || (_context$appHelper = context.appHelper) === null || _context$appHelper === void 0 ? void 0 : _context$appHelper.utils) || {};
  var action = utils.action;
  var app = utils === null || utils === void 0 || (_utils$getApp = utils.getApp) === null || _utils$getApp === void 0 ? void 0 : _utils$getApp.call(utils);
  request.setRequest(utils === null || utils === void 0 ? void 0 : utils.request);
  // 同步注入 isWebPos：订单 / 预约列表的退订仅在 WebPOS 才真正发请求（见 unsubscribeOsQuery）。
  setOsQueryWebPos(utils === null || utils === void 0 ? void 0 : utils.isWebPos);
  var _getRelationTables = useRequest(getRelationTables);
  /**
   * 当前 SalesManagement 是否处于激活态。
   * 由宿主通过 `engine.props.isActive` 注入（与 `engine.props.locale` 路径一致）；用于驱动
   * Find 扫码枪监听的开/关。useEngineContext 已把 engine 标为 any，无需再 cast。
   */
  var isActive = Boolean(context === null || context === void 0 || (_context$engine = context.engine) === null || _context$engine === void 0 || (_context$engine = _context$engine.props) === null || _context$engine === void 0 ? void 0 : _context$engine.isActive);

  /**
   * 订单（handle）与预约监控（monitor）两个视角都按 isActive 控制扫码枪监听。
   */
  var enableScanner = isActive;
  locales.init(salesManagementLocales, (context === null || context === void 0 || (_context$engine2 = context.engine) === null || _context$engine2 === void 0 || (_context$engine2 = _context$engine2.props) === null || _context$engine2 === void 0 ? void 0 : _context$engine2.locale) || 'en');
  useEffect(function () {
    if (perspective === 'monitor') {
      setTimeout(function () {
        action === null || action === void 0 || action({
          type: 'pisell1.registerBookingPlugin'
        });
      }, 300);
    }
  }, [perspective, action]);
  var params = app === null || app === void 0 || (_app$history = app.history) === null || _app$history === void 0 || (_app$history$useParam = _app$history.useParams) === null || _app$history$useParam === void 0 ? void 0 : _app$history$useParam.call(_app$history);
  var queryBusinessCode = useMemo(function () {
    if (typeof window === 'undefined') return undefined;
    var searchParams = new URLSearchParams(window.location.search || '');
    return searchParams.get('business_code') || undefined;
  }, []);
  var resolvedBusinessCode = useMemo(function () {
    var _ref, _ref2, _ref3, _props$businessCode;
    var raw = (_ref = (_ref2 = (_ref3 = (_props$businessCode = props.businessCode) !== null && _props$businessCode !== void 0 ? _props$businessCode : props.biz) !== null && _ref3 !== void 0 ? _ref3 : params === null || params === void 0 ? void 0 : params.business_code) !== null && _ref2 !== void 0 ? _ref2 : params === null || params === void 0 ? void 0 : params.biz) !== null && _ref !== void 0 ? _ref : queryBusinessCode;
    var value = raw != null ? String(raw).trim() : '';
    return value || undefined;
  }, [props.businessCode, props.biz, params === null || params === void 0 ? void 0 : params.business_code, params === null || params === void 0 ? void 0 : params.biz, queryBusinessCode]);
  var extParams = useMemo(function () {
    return {
      list: _objectSpread({
        business_code: resolvedBusinessCode
      }, mode !== undefined ? {
        mode: mode
      } : {})
    };
  }, [resolvedBusinessCode, mode]);
  var perspectivesRaw = usePerspectives({
    relationTables: Array.isArray(_getRelationTables.data) ? _getRelationTables.data : [],
    onOpenDetail: onOpenDetail,
    businessCode: resolvedBusinessCode
  });

  /**
   * 隐藏 toolBar 内置搜索框。
   *
   * perspective 的配置不是直接挂 `childComponentProps`，而是通过
   * `getChildComponentProps(ctx)` 工厂函数在运行时生成；所以这里必须**包装**
   * 该工厂，先调用原函数拿到运行时配置，再 patch `toolBar.search = false`。
   * （之前误以为是静态字段、直接 patch 会无效）
   *
   * 仅在 `hideToolbarSearch` 为 true 时浅拷贝一层避免污染原配置；
   * 否则直接复用 perspectivesRaw，零成本。
   */
  var perspectives = useMemo(function () {
    if (!hideToolbarSearch) return perspectivesRaw;
    return (perspectivesRaw || []).map(function (p) {
      var originalGet = p === null || p === void 0 ? void 0 : p.getChildComponentProps;
      if (typeof originalGet !== 'function') {
        var _p$childComponentProp;
        // 极少数 perspective 可能是静态字段写法，兜底处理一份
        return _objectSpread(_objectSpread({}, p), {}, {
          childComponentProps: _objectSpread(_objectSpread({}, (p === null || p === void 0 ? void 0 : p.childComponentProps) || {}), {}, {
            toolBar: _objectSpread(_objectSpread({}, (p === null || p === void 0 || (_p$childComponentProp = p.childComponentProps) === null || _p$childComponentProp === void 0 ? void 0 : _p$childComponentProp.toolBar) || {}), {}, {
              search: false
            })
          })
        });
      }
      return _objectSpread(_objectSpread({}, p), {}, {
        getChildComponentProps: function getChildComponentProps(ctx) {
          var result = originalGet(ctx) || {};
          return _objectSpread(_objectSpread({}, result), {}, {
            toolBar: _objectSpread(_objectSpread({}, result.toolBar || {}), {}, {
              search: false
            })
          });
        }
      });
    });
  }, [perspectivesRaw, hideToolbarSearch]);
  var gridDataProps = useSalesGridData({
    perspective: perspective,
    extParams: extParams,
    externalKeyword: keyword
  });
  var adapterLocale = (context === null || context === void 0 || (_context$engine3 = context.engine) === null || _context$engine3 === void 0 || (_context$engine3 = _context$engine3.props) === null || _context$engine3 === void 0 ? void 0 : _context$engine3.locale) === 'zh-CN' || (context === null || context === void 0 || (_context$engine4 = context.engine) === null || _context$engine4 === void 0 || (_context$engine4 = _context$engine4.props) === null || _context$engine4 === void 0 ? void 0 : _context$engine4.locale) === 'zh-HK' ? 'zh-cn' : (context === null || context === void 0 || (_context$engine5 = context.engine) === null || _context$engine5 === void 0 || (_context$engine5 = _context$engine5.props) === null || _context$engine5 === void 0 ? void 0 : _context$engine5.locale) === 'en' || (context === null || context === void 0 || (_context$engine6 = context.engine) === null || _context$engine6 === void 0 || (_context$engine6 = _context$engine6.props) === null || _context$engine6 === void 0 ? void 0 : _context$engine6.locale) === 'en-US' ? 'en' : 'zh-cn';

  // 与 materials getCurrentLocale 回退链一致：engine.props.locale → localStorage umi_locale → en-US，保证 Find/Filter/Sort 与 Quick Filter 同源语言
  var rawLocale = (context === null || context === void 0 || (_context$engine7 = context.engine) === null || _context$engine7 === void 0 || (_context$engine7 = _context$engine7.props) === null || _context$engine7 === void 0 ? void 0 : _context$engine7.locale) || (typeof localStorage !== 'undefined' ? localStorage.getItem('umi_locale') : null) || 'en-US';
  var materialsLocale = rawLocale === 'zh' ? 'zh-CN' : rawLocale;
  return /*#__PURE__*/React.createElement(PisellConfigProvider, {
    locale: materialsLocale
  }, /*#__PURE__*/React.createElement(LocalizationProvider, {
    dateAdapter: AdapterDayjs,
    adapterLocale: adapterLocale
  }, /*#__PURE__*/React.createElement("div", {
    style: Object.assign({}, {
      height: 0
    }, props === null || props === void 0 ? void 0 : props.style)
  }, /*#__PURE__*/React.createElement(PisellSalesGrid, {
    perspectives: perspectives,
    currentPerspective: perspective,
    extParams: extParams,
    __id: __id,
    data: gridDataProps.data,
    loading: gridDataProps.loading,
    total: gridDataProps.total,
    pagination: gridDataProps.pagination,
    onPageChange: gridDataProps.onPageChange,
    searchParams: gridDataProps.searchParams,
    onSearch: gridDataProps.onSearch,
    onReset: gridDataProps.onReset,
    selectedKeys: gridDataProps.selectedKeys,
    selectedRows: gridDataProps.selectedRows,
    onSelectionChange: gridDataProps.onSelectionChange,
    rowKey: gridDataProps.rowKey,
    onRefresh: gridDataProps.onRefresh,
    refreshAsync: gridDataProps.refreshAsync,
    enableScanner: enableScanner
  }))));
};
export default PisellSalesManagement;