import type { InjectionKey } from 'vue';

import { computed, provide, reactive } from 'vue';

export const uploadEditLoadingKey: InjectionKey<() => () => void> = Symbol('upload-edit-loading');

/** 每个窗口独立计数；并行附件、过期请求各自释放，不覆盖窗口原有加载状态。 */
export function provideUploadEditLoading() {
  const pending = reactive(new Set<symbol>());
  provide(uploadEditLoadingKey, () => {
    const token = Symbol('attachment-operation');
    pending.add(token);
    return () => {
      pending.delete(token);
    };
  });
  return computed(() => pending.size > 0);
}
