"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var pagination_exports = {};
__export(pagination_exports, {
  cursorHandlers: () => cursorHandlers,
  offsetHandlers: () => offsetHandlers
});
module.exports = __toCommonJS(pagination_exports);
var import_config = require("./config");
var import_deepEquals = require("./deepEquals");
var import_pageInfo = require("./pageInfo");
var import_types = require("./types");
function cursorHandlers({
  artifact,
  fetchUpdate: parentFetchUpdate,
  fetch: parentFetch,
  getState,
  getVariables,
  getSession
}) {
  const loadPage = async ({
    pageSizeVar,
    input,
    functionName,
    metadata = {},
    fetch,
    where
  }) => {
    const config = (0, import_config.getCurrentConfig)();
    const loadVariables = {
      ...getVariables(),
      ...input
    };
    if (!loadVariables[pageSizeVar] && !artifact.refetch.pageSize) {
      throw (0, import_pageInfo.missingPageSizeError)(functionName);
    }
    let isSinglePage = artifact.refetch?.mode === "SinglePage";
    await (isSinglePage ? parentFetch : parentFetchUpdate)(
      {
        variables: loadVariables,
        fetch,
        metadata,
        policy: isSinglePage ? artifact.policy : import_types.CachePolicy.NetworkOnly,
        session: await getSession()
      },
      isSinglePage ? [] : [where === "start" ? "prepend" : "append"]
    );
  };
  const getPageInfo = () => {
    return (0, import_pageInfo.extractPageInfo)(getState(), artifact.refetch?.path ?? []);
  };
  return {
    loadNextPage: async ({
      first,
      after,
      fetch,
      metadata
    } = {}) => {
      const currentPageInfo = getPageInfo();
      if (!currentPageInfo.hasNextPage) {
        return;
      }
      const input = {
        first: first ?? artifact.refetch.pageSize,
        after: after ?? currentPageInfo.endCursor,
        before: null,
        last: null
      };
      return await loadPage({
        pageSizeVar: "first",
        functionName: "loadNextPage",
        input,
        fetch,
        metadata,
        where: "end"
      });
    },
    loadPreviousPage: async ({
      last,
      before,
      fetch,
      metadata
    } = {}) => {
      const currentPageInfo = getPageInfo();
      if (!currentPageInfo.hasPreviousPage) {
        return;
      }
      const input = {
        before: before ?? currentPageInfo.startCursor,
        last: last ?? artifact.refetch.pageSize,
        first: null,
        after: null
      };
      return await loadPage({
        pageSizeVar: "last",
        functionName: "loadPreviousPage",
        input,
        fetch,
        metadata,
        where: "start"
      });
    },
    async fetch(args) {
      const { variables } = args ?? {};
      if (variables && !(0, import_deepEquals.deepEquals)(getVariables(), variables)) {
        return await parentFetch(args);
      }
      try {
        var currentPageInfo = (0, import_pageInfo.extractPageInfo)(getState(), artifact.refetch.path);
      } catch {
        return await parentFetch(args);
      }
      const queryVariables = {};
      const count = (0, import_pageInfo.countPage)(artifact.refetch.path.concat("edges"), getState()) || artifact.refetch.pageSize;
      if (count && count > artifact.refetch.pageSize) {
        if (currentPageInfo.hasPreviousPage && currentPageInfo.hasNextPage && !(variables?.["first"] && variables?.["after"] || variables?.["last"] && variables?.["before"])) {
          console.warn(`\u26A0\uFE0F Encountered a fetch() in the middle of the connection.
Make sure to pass a cursor value by hand that includes the current set (ie the entry before startCursor)
`);
        }
        if (!currentPageInfo.hasPreviousPage) {
          queryVariables["first"] = count;
          queryVariables["after"] = null;
          queryVariables["last"] = null;
          queryVariables["before"] = null;
        } else if (!currentPageInfo.hasNextPage) {
          queryVariables["last"] = count;
          queryVariables["first"] = null;
          queryVariables["after"] = null;
          queryVariables["before"] = null;
        }
      }
      Object.assign(queryVariables, variables ?? {});
      const result = await parentFetch({
        ...args,
        variables: queryVariables
      });
      return result;
    }
  };
}
function offsetHandlers({
  artifact,
  storeName,
  getState,
  getVariables,
  fetch: parentFetch,
  fetchUpdate: parentFetchUpdate,
  getSession
}) {
  let isSinglePage = artifact.refetch?.mode === "SinglePage";
  let getOffset = () => {
    let offset = artifact.refetch?.start || (0, import_pageInfo.countPage)(artifact.refetch.path, getState()) || artifact.refetch.pageSize;
    if (isSinglePage) {
      offset += getVariables()?.offset ?? 0;
    }
    return offset;
  };
  let currentOffset = getOffset() ?? 0;
  return {
    loadNextPage: async ({
      limit,
      offset,
      fetch,
      metadata
    } = {}) => {
      const queryVariables = {
        ...getVariables(),
        offset: offset ?? getOffset()
      };
      if (limit || limit === 0) {
        queryVariables.limit = limit;
      }
      if (!queryVariables.limit && !artifact.refetch.pageSize) {
        throw (0, import_pageInfo.missingPageSizeError)("loadNextPage");
      }
      const targetFetch = isSinglePage ? parentFetch : parentFetchUpdate;
      await targetFetch({
        variables: queryVariables,
        fetch,
        metadata,
        policy: isSinglePage ? artifact.policy : import_types.CachePolicy.NetworkOnly,
        session: await getSession()
      });
      const pageSize = queryVariables.limit || artifact.refetch.pageSize;
      currentOffset = offset + pageSize;
    },
    async fetch(params = {}) {
      const { variables } = params;
      if (variables && !(0, import_deepEquals.deepEquals)(getVariables(), variables)) {
        return parentFetch.call(this, params);
      }
      const count = currentOffset || getOffset();
      const queryVariables = {};
      if (!artifact.refetch.pageSize || count > artifact.refetch.pageSize) {
        queryVariables.limit = count;
      }
      return await parentFetch.call(this, {
        ...params,
        variables: queryVariables
      });
    }
  };
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  cursorHandlers,
  offsetHandlers
});
