import {
  Bitbar,
  DataSpaceSelector,
  MasterDetailTable,
  PanelContainer,
  PublicKeySelector,
  useDevtoolsDispatch,
  useDevtoolsState,
  useFeedMessages
} from "./chunk-VBEJ3GU4.mjs";

// src/panels/echo/FeedsPanel/FeedsPanel.tsx
import React, { useEffect, useMemo, useState } from "react";
import { generateName } from "@dxos/display-name";
import { Format } from "@dxos/echo/internal";
import { useClient } from "@dxos/react-client";
import { useDevtools, useStream } from "@dxos/react-client/devtools";
import { useContacts } from "@dxos/react-client/halo";
import { IconButton, Toolbar } from "@dxos/react-ui";
var FeedsPanel = (props) => {
  const devtoolsHost = useDevtools();
  const setContext = useDevtoolsDispatch();
  const state = useDevtoolsState();
  const space = props.space ?? state.space;
  const feedKey = state.feedKey;
  const feedMessages = useFeedMessages({
    feedKey,
    maxBlocks: 1e3
  }).reverse();
  const contacts = useContacts();
  const client = useClient();
  const [refreshCount, setRefreshCount] = useState(0);
  const feedKeys = [
    ...space?.internal.data.pipeline?.controlFeeds ?? [],
    ...space?.internal.data.pipeline?.dataFeeds ?? []
  ];
  const { feeds } = useStream(() => devtoolsHost.subscribeToFeeds({
    feedKeys
  }), {}, [
    refreshCount
  ]);
  const feed = feeds?.find((feed2) => feedKey && feed2.feedKey.equals(feedKey));
  const tableRows = mapToRows(client, space?.key, contacts, feedMessages);
  const key = feedKey ?? feedKeys[0];
  useEffect(() => {
    if (key && !feedKey) {
      handleSelect(key);
      setTimeout(() => {
        handleRefresh();
      });
    }
  }, [
    key
  ]);
  useEffect(() => {
    if (feedKey && feedKeys.length > 0 && !feedKeys.find((feed2) => feed2.equals(feedKey))) {
      handleSelect(feedKeys[0]);
    }
  }, [
    JSON.stringify(feedKeys),
    feedKey
  ]);
  const handleSelect = (feedKey2) => {
    setContext((state2) => ({
      ...state2,
      feedKey: feedKey2
    }));
    setTimeout(() => {
      handleRefresh();
    });
  };
  const handleRefresh = () => {
    setRefreshCount(refreshCount + 1);
  };
  const getLabel = (key2) => {
    const feed2 = feeds?.find((feed3) => feed3.feedKey.equals(key2));
    const feedLength = feed2 ? ` (${feed2.length})` : "";
    return `${formatIdentity(client, contacts, feed2?.owner)}${feedLength}`;
  };
  const properties = useMemo(() => [
    {
      name: "type",
      format: Format.TypeFormat.String
    },
    {
      name: "issuer",
      format: Format.TypeFormat.String
    },
    {
      name: "feedKey",
      format: Format.TypeFormat.JSON,
      size: 180
    },
    {
      name: "seq",
      format: Format.TypeFormat.Number,
      size: 80
    }
  ], []);
  const tableData = useMemo(() => {
    return tableRows.map((row) => ({
      id: `${row.feedKey.toHex()}-${row.seq}`,
      ...row
    }));
  }, [
    tableRows
  ]);
  return /* @__PURE__ */ React.createElement(PanelContainer, {
    toolbar: /* @__PURE__ */ React.createElement(Toolbar.Root, null, !props.space && /* @__PURE__ */ React.createElement(DataSpaceSelector, null), /* @__PURE__ */ React.createElement(PublicKeySelector, {
      placeholder: "Select feed",
      getLabel,
      keys: feedKeys,
      value: key,
      onChange: handleSelect
    }), /* @__PURE__ */ React.createElement(IconButton, {
      icon: "ph--arrow-clockwise--regular",
      iconOnly: true,
      label: "Refresh",
      onClick: handleRefresh
    }))
  }, /* @__PURE__ */ React.createElement("div", {
    className: "bs-full"
  }, /* @__PURE__ */ React.createElement(Bitbar, {
    value: feed?.downloaded ?? new Uint8Array(),
    length: feed?.length ?? 0,
    className: "m-4"
  }), /* @__PURE__ */ React.createElement(MasterDetailTable, {
    properties,
    data: tableData,
    detailsPosition: "bottom"
  })));
};
var mapToRows = (client, spaceKey, contacts, blocks) => {
  return blocks.map((block) => {
    const credential = block.data.payload.credential?.credential;
    const type = credential?.subject?.assertion?.["@type"] ?? "unknown_type";
    const issuerKeys = credential ? {
      identity: credential.issuer,
      device: credential.proof.signer
    } : void 0;
    return {
      type,
      issuer: issuerKeys ? formatIdentity(client, contacts, issuerKeys) : "unknown",
      ...block
    };
  });
};
var formatIdentity = (client, contacts, identityInfo) => {
  if (!identityInfo) {
    return "unknown";
  }
  let identityName;
  if (client.halo.identity.get()?.identityKey?.equals(identityInfo.identity)) {
    identityName = client.halo.device?.deviceKey.equals(identityInfo.device) ? "this device" : "my device";
  } else {
    const ownerContact = identityInfo && contacts.find((contact) => contact.identityKey.equals(identityInfo.identity));
    identityName = ownerContact?.profile?.displayName ?? generateName(identityInfo.identity.toHex());
  }
  return `${identityName} (${identityInfo.device.truncate()})`;
};

// src/panels/echo/FeedsPanel/index.ts
var FeedsPanel_default = FeedsPanel;
export {
  FeedsPanel_default as default
};
//# sourceMappingURL=FeedsPanel-WMH4342D.mjs.map
