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, { useMemo } from 'react';
import { useMemoizedFn, useRequest } from 'ahooks';
import { isString, isBoolean } from '@pisell/utils';
import DataSourceContext from "./DataSourceContext";
import useActions from "../../hooks/useActions";
import useVariables from "../../hooks/useVariables";
import useSetRequest from "../../../../hooks/useSetRequest";
import useDataSource from "../../hooks/useDataSource";

/**
 * @title: DataSource Provider
 * @description: 提供页面配置的数据源，支持外部传入Promise接口、自动调用配置和直接数据
 */
var DataSourceProvider = function DataSourceProvider(props) {
  var children = props.children,
    propsDataSource = props.dataSource,
    currentValue = props.currentValue,
    extraParams = props.extraParams,
    externalActions = props.actions,
    overrideData = props.overrideData,
    autoRun = props.autoRun,
    requestOptions = props.requestOptions,
    __designMode = props.__designMode;
  var _useDataSource = useDataSource(),
    ctxGetCurrentRecord = _useDataSource.getCurrentRecord;
  useSetRequest();
  var _useVariables = useVariables(),
    parseVariable = _useVariables.parseVariable;
  var getCurrentRecord = useMemoizedFn(function () {
    return (parseVariable === null || parseVariable === void 0 ? void 0 : parseVariable(currentValue, {})) || ctxGetCurrentRecord();
  });
  var dataSource = isString(propsDataSource) ? parseVariable === null || parseVariable === void 0 ? void 0 : parseVariable(propsDataSource) : propsDataSource;

  // 处理标准数据源
  var standardActions = useActions({
    dataSource: dataSource,
    currentValue: currentValue,
    __designMode: __designMode,
    extraParams: extraParams
  });

  // 使用外部Promise或标准actions
  var list = (externalActions === null || externalActions === void 0 ? void 0 : externalActions.list) || standardActions.list;
  var get = (externalActions === null || externalActions === void 0 ? void 0 : externalActions.get) || standardActions.get;
  var update = (externalActions === null || externalActions === void 0 ? void 0 : externalActions.update) || standardActions.update;
  var create = (externalActions === null || externalActions === void 0 ? void 0 : externalActions.create) || standardActions.create;
  var destroy = (externalActions === null || externalActions === void 0 ? void 0 : externalActions.destroy) || standardActions.destroy;
  var listResult = useRequest(list, _objectSpread({
    manual: isBoolean(autoRun === null || autoRun === void 0 ? void 0 : autoRun.list) ? !(autoRun !== null && autoRun !== void 0 && autoRun.list) : true
  }, requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.list));
  var getResult = useRequest(get, {
    manual: isBoolean(autoRun === null || autoRun === void 0 ? void 0 : autoRun.get) ? !(autoRun !== null && autoRun !== void 0 && autoRun.get) : true
  });
  var destroyResult = useRequest(destroy, {
    manual: true
  });
  var updateResult = useRequest(update, {
    manual: true
  });
  var createResult = useRequest(create, {
    manual: true
  });
  var value = useMemo(function () {
    return {
      __designMode: __designMode,
      dataSource: dataSource,
      getCurrentRecord: getCurrentRecord,
      list: _objectSpread(_objectSpread({}, listResult), {}, {
        data: (overrideData === null || overrideData === void 0 ? void 0 : overrideData.list) || listResult.data
      }),
      get: _objectSpread(_objectSpread({}, getResult), {}, {
        data: (overrideData === null || overrideData === void 0 ? void 0 : overrideData.get) || getResult.data
      }),
      destroy: destroyResult,
      update: updateResult,
      create: createResult
    };
  }, [dataSource, listResult, destroyResult, getResult, updateResult, createResult, overrideData, __designMode]);
  return /*#__PURE__*/React.createElement(DataSourceContext.Provider, {
    value: value
  }, children);
};
export default DataSourceProvider;