"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports.useAmbassadorCollection = useAmbassadorCollection;
exports.useCreateAmbassadorCollection = useCreateAmbassadorCollection;
var _identity = _interopRequireDefault(require("lodash/identity"));
var _providers = require("../providers");
var _useCollection = require("./useCollection");
var _react = require("react");
// TODO: should be exposed by Ambassador

async function fetchData({
  config,
  wixPatternsQuery,
  httpClient,
  errorHandler
}) {
  const {
    ambassador
  } = config;
  const transformPlatformizedQuery = config.transformPlatformizedQuery || _identity.default;
  const platformizedQuery = defaultPlatformizedQuery(wixPatternsQuery);
  const query = await transformPlatformizedQuery(platformizedQuery, wixPatternsQuery);
  return errorHandler == null ? void 0 : errorHandler.withErrorHandler(() => httpClient.request(ambassador.queryEntities({
    query
  })), {});
}
function getOffsetConfig({
  config,
  httpClient,
  errorHandler
}) {
  return {
    queryName: config.ambassador.fqdn,
    ...config,
    paginationMode: 'offset',
    fetchData: async query => {
      const response = await fetchData({
        config,
        wixPatternsQuery: query,
        httpClient,
        errorHandler
      });
      return {
        items: (response == null ? void 0 : response.data.entities) ?? [],
        total: (response == null ? void 0 : response.data.metadata.total) ?? 0
      };
    }
  };
}
function getCursorConfig({
  config,
  httpClient,
  errorHandler
}) {
  const {
    ambassador
  } = config;
  return {
    queryName: ambassador.fqdn,
    ...config,
    paginationMode: 'cursor',
    fetchData: async query => {
      const response = await fetchData({
        config,
        wixPatternsQuery: query,
        httpClient,
        errorHandler
      });
      return {
        items: (response == null ? void 0 : response.data.entities) ?? [],
        cursor: response == null ? void 0 : response.data.metadata.cursors.next,
        total: (response == null ? void 0 : response.data.metadata.total) ?? 0
      };
    }
  };
}
function useCreateAmbassadorCollection() {
  const createCollection = (0, _useCollection.useCreateCollection)();
  const {
    httpClient,
    errorHandler
  } = (0, _providers.useWixPatternsContainer)();
  return config => {
    const collectionConfig = config.ambassador.queryEntities.paginationMode === 'offset' ? getOffsetConfig({
      config,
      httpClient,
      errorHandler
    }) : getCursorConfig({
      config,
      httpClient,
      errorHandler
    });
    return createCollection(collectionConfig);
  };
}
function useAmbassadorCollection(config) {
  const createAmbassadorCollection = useCreateAmbassadorCollection();
  const [collection] = (0, _react.useState)(() => createAmbassadorCollection(config));
  return collection;
}
function defaultPlatformizedQuery(wixPatternsQuery) {
  var _wixPatternsQuery$sor;
  return {
    filter: {},
    sort: (_wixPatternsQuery$sor = wixPatternsQuery.sort) == null ? void 0 : _wixPatternsQuery$sor.map(sort => ({
      fieldName: sort.fieldName,
      order: sort.order.toUpperCase()
    })),
    cursorPaging: {
      cursor: wixPatternsQuery.cursor,
      limit: wixPatternsQuery.limit
    }
  };
}
//# sourceMappingURL=useAmbassadorCollection.js.map