"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.TableOfContents = void 0;
const tslib_1 = require("tslib");
const React = tslib_1.__importStar(require("react"));
const urql_1 = require("urql");
const TableOfContents_1 = require("../components/TableOfContents");
const Skeleton_1 = require("../components/TableOfContents/Skeleton");
const Provider_1 = require("./Provider");
const query = `
query ElementsTableOfContents(
  $workspaceId: Int!
  $projectSlug: String
  $branchSlug: String
  $baseUri: String
) {
  sl_search_nodes(
    args: { workspaceid: $workspaceId, search: "" }
    where: {
      branchNode: { baseUri: { _ilike: $baseUri }, branch: { slug: { _eq: $branchSlug } } }
      project: { slug: { _eq: $projectSlug } }
    }
  ) {
    id

    branchNode {
      id

      node {
        id
        uri
      }
  
      snapshot {
        id
        name
        type
      }
    }
  }
}
`;
const workspaceQuery = `
query ElementsWorkspaceBySlug($slug: String!) {
  workspaces(limit: 1, where: { slug: { _eq: $slug } }) {
    id
  }
}
`;
exports.TableOfContents = ({ className, filter }) => {
    var _a, _b;
    const info = React.useContext(Provider_1.ActiveInfoContext);
    const [{ data: workspaceData, fetching: workspaceFetching }] = urql_1.useQuery({
        query: workspaceQuery,
        variables: {
            slug: info.workspace,
        },
    });
    const workspaceId = (_b = (_a = workspaceData === null || workspaceData === void 0 ? void 0 : workspaceData.workspaces) === null || _a === void 0 ? void 0 : _a[0]) === null || _b === void 0 ? void 0 : _b.id;
    const [{ data, fetching }] = urql_1.useQuery({
        query,
        pause: !workspaceId,
        variables: {
            workspaceId,
            projectSlug: info.project,
            branchSlug: info.branch,
            baseUri: (filter === null || filter === void 0 ? void 0 : filter.nodeUri) ? `${filter.nodeUri}%` : undefined,
        },
    });
    if (workspaceFetching || fetching) {
        return React.createElement(Skeleton_1.TableOfContentsSkeleton, { className: className });
    }
    return (React.createElement(TableOfContents_1.TableOfContents, { className: className, nodes: ((data === null || data === void 0 ? void 0 : data.sl_search_nodes) || []).map(({ branchNode }) => branchNode) }));
};
//# sourceMappingURL=TableOfContents.js.map