// API 基础配置
export const API_CONFIG = {
  // nginx
  NGINX: 'http://localhost:8099',
  // 测试环境
  LOCAL: 'http://localhost:8080'
};

interface ApiResponse {
  code: number;
  message: string;
  data: any;
}

// 当前环境的基础URL
export const BASE_URL = API_CONFIG.LOCAL;

// 导出便捷的 URL 构建函数
export const buildApiUrl = (path: string) => {
  return `${BASE_URL}${path}`;
};

export const modelApi = {
  //http://localhost:8080/fileStorage/deleteFileById
  fileStorage_deleteFileById_get(endJson: any): Promise<ApiResponse> {
    return fetch(
      buildApiUrl('/fileStorage/deleteFileById') +
        '?' +
        Object.keys(endJson)
          .map((key) => key + '=' + endJson[key])
          .join('&'),
      {
        method: 'GET',
        headers: {
          'Content-Type': 'application/json',
          Accept: '*/*'
        }
      }
    ).then((response) => {
      if (!response.ok) throw new Error(`HTTP error! status: ${response.status}`);
      return response.json();
    });
  },

  fileStorage_editFileNameById_post(endJson: any): Promise<ApiResponse> {
    return fetch(buildApiUrl('/fileStorage/editFileNameById'), {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
        Accept: '*/*'
      },
      body: JSON.stringify(endJson)
    }).then((response) => {
      if (!response.ok) throw new Error(`HTTP error! status: ${response.status}`);
      return response.json();
    });
  },

  fileStorage_file_list_post(endJson: any): Promise<ApiResponse> {
    return fetch(buildApiUrl('/fileStorage/file/list'), {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
        Accept: '*/*'
      },
      body: JSON.stringify(endJson)
    }).then((response) => {
      if (!response.ok) throw new Error(`HTTP error! status: ${response.status}`);
      return response.json();
    });
  },

  // 获取节点信息
  node_nrt_get() {
    return fetch(buildApiUrl('/node/nrt'), {
      method: 'GET',
      headers: {
        'Content-Type': 'application/json',
        Accept: '*/*'
      }
    });
  },

  // 加载模型数据
  model_getModelData_post(endJson: any): Promise<ApiResponse> {
    return fetch(buildApiUrl('/data/model/getModelData'), {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
        Accept: '*/*'
      },
      body: JSON.stringify(endJson)
    }).then((response) => {
      if (!response.ok) throw new Error(`HTTP error! status: ${response.status}`);
      return response.json();
    });
  },

  // 加载模型数据
  saveOrUpdate_modelData_post(endJson: any): Promise<ApiResponse> {
    return fetch(buildApiUrl('/data/model/saveOrUpdate/modelData'), {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
        Accept: '*/*'
      },
      body: JSON.stringify(endJson)
    }).then((response) => {
      if (!response.ok) throw new Error(`HTTP error! status: ${response.status}`);
      return response.json();
    });
  }
};
