'use strict';

var request = require('../utils/request.js');

const useBaseApi = (module, options = {}) => {
  const baseUrl = `/api/${module}/`;
  const request$1 = (config, cancel = false) => {
    if (cancel) {
      request.cancelRequest(config.url || "");
      return Promise.resolve({});
    }
    config = { ...config, ...options };
    return request.service(config);
  };
  return {
    baseUrl,
    request: request$1,
    pcomm: function(data, action, cancel = false) {
      return request$1(
        {
          url: baseUrl + action,
          method: "post",
          data
        },
        cancel
      );
    },
    post: function(data, action, cancel = false) {
      return request$1(
        {
          url: baseUrl + action,
          method: "post",
          data
        },
        cancel
      );
    },
    gcomm: function(params, action, cancel = false) {
      return request$1(
        {
          url: baseUrl + action,
          method: "get",
          params
        },
        cancel
      );
    },
    download: function(data, action, cancel = false) {
      return request$1(
        {
          responseType: "arraybuffer",
          url: baseUrl + action,
          method: "post",
          data
        },
        cancel
      );
    },
    get: function(params, action, cancel = false) {
      return request$1(
        {
          url: baseUrl + action,
          method: "get",
          params
        },
        cancel
      );
    },
    page: function(data, cancel = false) {
      return request$1(
        {
          url: baseUrl + "page",
          method: "post",
          data
        },
        cancel
      );
    },
    detail: function(id, cancel = false) {
      return request$1(
        {
          url: baseUrl + "detail",
          method: "get",
          data: { id }
        },
        cancel
      );
    },
    dropdownData: function(data, cancel = false) {
      return request$1(
        {
          url: baseUrl + "dropdownData",
          method: "post",
          data
        },
        cancel
      );
    },
    add: function(data, cancel = false) {
      return request$1(
        {
          url: baseUrl + "add",
          method: "post",
          data
        },
        cancel
      );
    },
    update: function(data, cancel = false) {
      return request$1(
        {
          url: baseUrl + "update",
          method: "post",
          data
        },
        cancel
      );
    },
    setStatus: function(data, cancel = false) {
      return request$1(
        {
          url: baseUrl + "setStatus",
          method: "post",
          data
        },
        cancel
      );
    },
    delete: function(data, cancel = false) {
      return request$1(
        {
          url: baseUrl + "delete",
          method: "post",
          data
        },
        cancel
      );
    },
    batchDelete: function(data, cancel = false) {
      return request$1(
        {
          url: baseUrl + "batchDelete",
          method: "post",
          data
        },
        cancel
      );
    },
    exportData: function(data, cancel = false) {
      return request$1(
        {
          responseType: "arraybuffer",
          url: baseUrl + "export",
          method: "post",
          data
        },
        cancel
      );
    },
    downloadTemplate: function(cancel = false) {
      return request$1(
        {
          responseType: "arraybuffer",
          url: baseUrl + "import",
          method: "get"
        },
        cancel
      );
    },
    importData: function(file, cancel = false) {
      const formData = new FormData();
      formData.append("file", file);
      return request$1(
        {
          headers: { "Content-Type": "multipart/form-data;charset=UTF-8" },
          responseType: "arraybuffer",
          url: baseUrl + "import",
          method: "post",
          data: formData
        },
        cancel
      );
    },
    uploadFile: function(params, action, cancel = false) {
      const formData = new FormData();
      formData.append("file", params.file);
      if (params.data) {
        Object.keys(params.data).forEach((key) => {
          const value = params.data[key];
          if (Array.isArray(value)) {
            value.forEach((item) => formData.append(`${key}[]`, item));
            return;
          }
          formData.append(key, params.data[key]);
        });
      }
      return request$1(
        {
          url: baseUrl + action,
          method: "POST",
          data: formData,
          headers: {
            "Content-Type": "multipart/form-data;charset=UTF-8",
            ignoreCancelToken: true
          }
        },
        cancel
      );
    }
  };
};
const useApi = (url, options = {}) => {
  const baseUrl = url || "";
  const request$1 = (config, cancel = false) => {
    if (cancel) {
      request.cancelRequest(config.url || "");
      return Promise.resolve({});
    }
    config = { ...config, ...options };
    return request.service(config);
  };
  return {
    baseUrl,
    request: request$1,
    post: function(data, action, cancel = false) {
      return request$1(
        {
          url: baseUrl + action,
          method: "post",
          data
        },
        cancel
      );
    },
    get: function(params, action, cancel = false) {
      return request$1(
        {
          url: baseUrl + action,
          method: "get",
          params
        },
        cancel
      );
    }
  };
};

exports.useApi = useApi;
exports.useBaseApi = useBaseApi;
