import { fetchResource } from "./api";

let smartOperationReportConfigCache = null;
let smartOperationReportConfigPromise = null;

export async function loadSmartOperationReportConfig(force = false) {
  if (!force && smartOperationReportConfigCache) {
    return smartOperationReportConfigCache;
  }
  if (!force && smartOperationReportConfigPromise) {
    return smartOperationReportConfigPromise;
  }

  smartOperationReportConfigPromise = fetchResource("/system-extension/smart-operation-report", {
    config_code: "SMART_OPERATION_REPORT_ENABLED",
    config_name: "对接智能报工小程序",
    enabled: true,
    config_value: "开启",
    remark: ""
  }).then((config) => {
    smartOperationReportConfigCache = {
      ...config,
      enabled: config?.enabled !== false
    };
    smartOperationReportConfigPromise = null;
    return smartOperationReportConfigCache;
  });
  return smartOperationReportConfigPromise;
}

export function resetSmartOperationReportConfigCache(config = null) {
  smartOperationReportConfigCache = config;
  smartOperationReportConfigPromise = null;
}
