import type { FilePermissionView } from '#/components/Jnpf/upload/src/filePermissionContext';

import { inject, onBeforeUnmount, ref, watch } from 'vue';

import { getFlowFilePermissions } from '#/api/workFlow/task';
import { workflowFileSubmitPendingKey } from '#/components/Jnpf/upload/src/filePermissionContext';

/** 在流程状态原地切换后立即刷新权限，并隔离表单切换期间返回的旧响应。 */
export async function refreshConfiguredFilePermissions(config: () => any) {
  const currentConfig = config();
  const current = currentConfig?.filePermissions as FilePermissionView | undefined;
  if (!current?.enabled || !current.taskId || !currentConfig?.id) return false;
  const recordId = currentConfig.id;
  const taskId = current.taskId;
  const formId = currentConfig.formId;
  const response = await getFlowFilePermissions(taskId, { formId });
  const latestConfig = config();
  if (
    latestConfig !== currentConfig ||
    latestConfig?.id !== recordId ||
    latestConfig?.formId !== formId ||
    latestConfig?.filePermissions?.taskId !== taskId ||
    latestConfig?.filePermissions?.enabled !== true
  ) {
    return false;
  }
  const next = response.data as FilePermissionView;
  if (!next || next.taskId !== taskId || next.formId !== formId || next.enabled !== true) {
    throw new Error('file permission context changed');
  }
  latestConfig.filePermissions = next;
  return true;
}

/** 只刷新当前实例的文件权限；关闭、切换记录后不能采纳旧请求。 */
export function useFilePermissions(config: () => any) {
  const submitting = inject(workflowFileSubmitPendingKey, () => false);
  const permissions = ref<FilePermissionView>();
  let generation = 0;
  let pending = false;
  let disposed = false;

  watch(
    () => [config()?.id, config()?.formId, config()?.filePermissions],
    () => {
      generation++;
      pending = false;
      permissions.value = config()?.filePermissions ?? undefined;
    },
    { immediate: true },
  );

  watch(
    submitting,
    () => {
      if (!permissions.value?.enabled) return;
      generation++;
      pending = false;
      if (!submitting()) void refresh();
    },
    { flush: 'sync' },
  );

  async function refresh() {
    const current = permissions.value;
    if (disposed || pending || submitting() || !current?.enabled || !config()?.id) return;
    const request = generation;
    // 详情入口可能使用业务记录别名，刷新必须使用服务端确认的真实流程实例。
    const id = current.taskId;
    const formId = config().formId;
    pending = true;
    try {
      const response = await getFlowFilePermissions(id, { formId });
      if (disposed || request !== generation) return;
      const next = response.data as FilePermissionView;
      if (!next || next.taskId !== current.taskId || next.formId !== formId || next.enabled !== true) throw new Error('file permission context changed');
      permissions.value = next;
    } catch {
      if (!disposed && request === generation) permissions.value = { ...current, status: 'error', fields: {} };
    } finally {
      if (request === generation) pending = false;
    }
  }

  const timer = setInterval(() => {
    if (!document.hidden) void refresh();
  }, 15_000);
  window.addEventListener('focus', refresh);
  onBeforeUnmount(() => {
    disposed = true;
    generation++;
    clearInterval(timer);
    window.removeEventListener('focus', refresh);
  });
  return permissions;
}
