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 } 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, _context$engine, _context$engine2, _window, _context$engine3, _context$engine4, _context$engine5, _context$engine6, _context$engine7;
  var perspective = props.perspective,
    __id = props.__id;
  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;
  request.setRequest(utils === null || utils === void 0 ? void 0 : utils.request);
  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]);

  //@ts-ignore
  var useParams = (_window = window) === null || _window === void 0 || (_window = _window.app) === null || _window === void 0 || (_window = _window.history) === null || _window === void 0 ? void 0 : _window.useParams;
  //@ts-ignore
  var params = useParams === null || useParams === void 0 ? void 0 : useParams();
  var queryBusinessCode = useMemo(function () {
    if (typeof window === 'undefined') return undefined;
    var searchParams = new URLSearchParams(window.location.search || '');
    return searchParams.get('business_code') || undefined;
  }, []);
  var extParams = useMemo(function () {
    return {
      list: {
        business_code: props.biz || (params === null || params === void 0 ? void 0 : params.biz) || queryBusinessCode
      }
    };
  }, [props.biz, params === null || params === void 0 ? void 0 : params.biz, queryBusinessCode]);
  var perspectives = usePerspectives({
    relationTables: Array.isArray(_getRelationTables.data) ? _getRelationTables.data : []
  });
  var gridDataProps = useSalesGridData({
    perspective: perspective,
    extParams: extParams
  });
  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;