const COMPOSER_SHADOW_PREFIX = 'easyflow:agent-composer-shadow';
const RUNTIME_STORAGE_PREFIX = 'easyflow:agent-chat-runtime';

type AccountIdentity =
  | null
  | undefined
  | {
      id?: number | string;
      tenantId?: number | string;
    };

const clearListeners = new Set<(identity: string) => void>();

/**
 * 生成聊天本地缓存使用的账号隔离标识。
 */
export function resolveAgentChatIdentity(account: AccountIdentity) {
  if (!account?.id) {
    return '';
  }
  return `${String(account.tenantId || 'default')}:${String(account.id)}`;
}

/**
 * 注册账号聊天缓存清理监听器。
 */
export function onAgentChatCacheClear(listener: (identity: string) => void) {
  clearListeners.add(listener);
  return () => clearListeners.delete(listener);
}

/**
 * 清理指定账号的草稿影子和运行快照。
 */
export function clearAgentChatBrowserCache(account: AccountIdentity) {
  const identity = resolveAgentChatIdentity(account);
  if (!identity) {
    return;
  }
  removeStorageEntries(safeStorage('localStorage'), [
    `${COMPOSER_SHADOW_PREFIX}:${identity}:`,
  ]);
  removeStorageEntries(safeStorage('sessionStorage'), [
    `${RUNTIME_STORAGE_PREFIX}:${identity}:`,
  ]);
  for (const listener of clearListeners) {
    listener(identity);
  }
}

/** 草稿影子存储前缀。 */
export { COMPOSER_SHADOW_PREFIX, RUNTIME_STORAGE_PREFIX };

function removeStorageEntries(
  storage: Storage | undefined,
  prefixes: string[],
) {
  if (!storage) {
    return;
  }
  try {
    const keys: string[] = [];
    for (let index = 0; index < storage.length; index++) {
      const key = storage.key(index);
      if (key && prefixes.some((prefix) => key.startsWith(prefix))) {
        keys.push(key);
      }
    }
    for (const key of keys) {
      storage.removeItem(key);
    }
  } catch {
    // 浏览器禁用存储时，登录退出流程仍应继续。
  }
}

function safeStorage(type: 'localStorage' | 'sessionStorage') {
  try {
    return globalThis[type];
  } catch {
    return undefined;
  }
}
