function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
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(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); }
function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
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;