import { c as _c } from "react-compiler-runtime";
import { useCallback, useEffect, useMemo, useRef, useState } from 'react';
import { isFeedbackSurveyDisabled } from 'src/services/analytics/config.js';
import { checkStatsigFeatureGate_CACHED_MAY_BE_STALE } from 'src/services/analytics/growthbook.js';
import { type AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS, logEvent } from 'src/services/analytics/index.js';
import { shouldUseSessionMemoryCompaction } from '../../services/compact/sessionMemoryCompact.js';
import type { Message } from '../../types/message.js';
import { isEnvTruthy } from '../../utils/envUtils.js';
import { isCompactBoundaryMessage } from '../../utils/messages.js';
import { logOTelEvent } from '../../utils/telemetry/events.js';
import { useSurveyState } from './useSurveyState.js';
import type { FeedbackSurveyResponse } from './utils.js';
const HIDE_THANKS_AFTER_MS = 3000;
const POST_COMPACT_SURVEY_GATE = 'tengu_post_compact_survey';
const SURVEY_PROBABILITY = 0.2; // Show survey 20% of the time after compaction

function hasMessageAfterBoundary(messages: Message[], boundaryUuid: string): boolean {
  const boundaryIndex = messages.findIndex(msg => msg.uuid === boundaryUuid);
  if (boundaryIndex === -1) {
    return false;
  }

  // Check if there's a user or assistant message after the boundary
  for (let i = boundaryIndex + 1; i < messages.length; i++) {
    const msg = messages[i];
    if (msg && (msg.type === 'user' || msg.type === 'assistant')) {
      return true;
    }
  }
  return false;
}
export function usePostCompactSurvey(messages, isLoading, t0, t1) {
  const $ = _c(23);
  const hasActivePrompt = t0 === undefined ? false : t0;
  let t2;
  if ($[0] !== t1) {
    t2 = t1 === undefined ? {} : t1;
    $[0] = t1;
    $[1] = t2;
  } else {
    t2 = $[1];
  }
  const {
    enabled: t3
  } = t2;
  const enabled = t3 === undefined ? true : t3;
  const [gateEnabled, setGateEnabled] = useState(null);
  let t4;
  if ($[2] === Symbol.for("react.memo_cache_sentinel")) {
    t4 = new Set();
    $[2] = t4;
  } else {
    t4 = $[2];
  }
  const seenCompactBoundaries = useRef(t4);
  const pendingCompactBoundaryUuid = useRef(null);
  const onOpen = _temp;
  const onSelect = _temp2;
  let t5;
  if ($[3] === Symbol.for("react.memo_cache_sentinel")) {
    t5 = {
      hideThanksAfterMs: HIDE_THANKS_AFTER_MS,
      onOpen,
      onSelect
    };
    $[3] = t5;
  } else {
    t5 = $[3];
  }
  const {
    state,
    lastResponse,
    open,
    handleSelect
  } = useSurveyState(t5);
  let t6;
  let t7;
  if ($[4] !== enabled) {
    t6 = () => {
      if (!enabled) {
        return;
      }
      setGateEnabled(checkStatsigFeatureGate_CACHED_MAY_BE_STALE(POST_COMPACT_SURVEY_GATE));
    };
    t7 = [enabled];
    $[4] = enabled;
    $[5] = t6;
    $[6] = t7;
  } else {
    t6 = $[5];
    t7 = $[6];
  }
  useEffect(t6, t7);
  let t8;
  if ($[7] !== messages) {
    t8 = new Set(messages.filter(_temp3).map(_temp4));
    $[7] = messages;
    $[8] = t8;
  } else {
    t8 = $[8];
  }
  const currentCompactBoundaries = t8;
  let t10;
  let t9;
  if ($[9] !== currentCompactBoundaries || $[10] !== enabled || $[11] !== gateEnabled || $[12] !== hasActivePrompt || $[13] !== isLoading || $[14] !== messages || $[15] !== open || $[16] !== state) {
    t9 = () => {
      if (!enabled) {
        return;
      }
      if (state !== "closed" || isLoading) {
        return;
      }
      if (hasActivePrompt) {
        return;
      }
      if (gateEnabled !== true) {
        return;
      }
      if (isFeedbackSurveyDisabled()) {
        return;
      }
      if (isEnvTruthy(process.env.CLAUDE_CODE_DISABLE_FEEDBACK_SURVEY)) {
        return;
      }
      if (pendingCompactBoundaryUuid.current !== null) {
        if (hasMessageAfterBoundary(messages, pendingCompactBoundaryUuid.current)) {
          pendingCompactBoundaryUuid.current = null;
          if (Math.random() < SURVEY_PROBABILITY) {
            open();
          }
          return;
        }
      }
      const newBoundaries = Array.from(currentCompactBoundaries).filter(uuid => !seenCompactBoundaries.current.has(uuid));
      if (newBoundaries.length > 0) {
        seenCompactBoundaries.current = new Set(currentCompactBoundaries);
        pendingCompactBoundaryUuid.current = newBoundaries[newBoundaries.length - 1];
      }
    };
    t10 = [enabled, currentCompactBoundaries, state, isLoading, hasActivePrompt, gateEnabled, messages, open];
    $[9] = currentCompactBoundaries;
    $[10] = enabled;
    $[11] = gateEnabled;
    $[12] = hasActivePrompt;
    $[13] = isLoading;
    $[14] = messages;
    $[15] = open;
    $[16] = state;
    $[17] = t10;
    $[18] = t9;
  } else {
    t10 = $[17];
    t9 = $[18];
  }
  useEffect(t9, t10);
  let t11;
  if ($[19] !== handleSelect || $[20] !== lastResponse || $[21] !== state) {
    t11 = {
      state,
      lastResponse,
      handleSelect
    };
    $[19] = handleSelect;
    $[20] = lastResponse;
    $[21] = state;
    $[22] = t11;
  } else {
    t11 = $[22];
  }
  return t11;
}
function _temp4(msg_0) {
  return msg_0.uuid;
}
function _temp3(msg) {
  return isCompactBoundaryMessage(msg);
}
function _temp2(appearanceId_0, selected) {
  const smCompactionEnabled_0 = shouldUseSessionMemoryCompaction();
  logEvent("tengu_post_compact_survey_event", {
    event_type: "responded" as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS,
    appearance_id: appearanceId_0 as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS,
    response: selected as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS,
    session_memory_compaction_enabled: smCompactionEnabled_0 as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS
  });
  logOTelEvent("feedback_survey", {
    event_type: "responded",
    appearance_id: appearanceId_0,
    response: selected,
    survey_type: "post_compact"
  });
}
function _temp(appearanceId) {
  const smCompactionEnabled = shouldUseSessionMemoryCompaction();
  logEvent("tengu_post_compact_survey_event", {
    event_type: "appeared" as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS,
    appearance_id: appearanceId as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS,
    session_memory_compaction_enabled: smCompactionEnabled as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS
  });
  logOTelEvent("feedback_survey", {
    event_type: "appeared",
    appearance_id: appearanceId,
    survey_type: "post_compact"
  });
}
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJuYW1lcyI6WyJ1c2VDYWxsYmFjayIsInVzZUVmZmVjdCIsInVzZU1lbW8iLCJ1c2VSZWYiLCJ1c2VTdGF0ZSIsImlzRmVlZGJhY2tTdXJ2ZXlEaXNhYmxlZCIsImNoZWNrU3RhdHNpZ0ZlYXR1cmVHYXRlX0NBQ0hFRF9NQVlfQkVfU1RBTEUiLCJBbmFseXRpY3NNZXRhZGF0YV9JX1ZFUklGSUVEX1RISVNfSVNfTk9UX0NPREVfT1JfRklMRVBBVEhTIiwibG9nRXZlbnQiLCJzaG91bGRVc2VTZXNzaW9uTWVtb3J5Q29tcGFjdGlvbiIsIk1lc3NhZ2UiLCJpc0VudlRydXRoeSIsImlzQ29tcGFjdEJvdW5kYXJ5TWVzc2FnZSIsImxvZ09UZWxFdmVudCIsInVzZVN1cnZleVN0YXRlIiwiRmVlZGJhY2tTdXJ2ZXlSZXNwb25zZSIsIkhJREVfVEhBTktTX0FGVEVSX01TIiwiUE9TVF9DT01QQUNUX1NVUlZFWV9HQVRFIiwiU1VSVkVZX1BST0JBQklMSVRZIiwiaGFzTWVzc2FnZUFmdGVyQm91bmRhcnkiLCJtZXNzYWdlcyIsImJvdW5kYXJ5VXVpZCIsImJvdW5kYXJ5SW5kZXgiLCJmaW5kSW5kZXgiLCJtc2ciLCJ1dWlkIiwiaSIsImxlbmd0aCIsInR5cGUiLCJ1c2VQb3N0Q29tcGFjdFN1cnZleSIsImlzTG9hZGluZyIsInQwIiwidDEiLCIkIiwiX2MiLCJoYXNBY3RpdmVQcm9tcHQiLCJ1bmRlZmluZWQiLCJ0MiIsImVuYWJsZWQiLCJ0MyIsImdhdGVFbmFibGVkIiwic2V0R2F0ZUV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