import { api } from '../constants/config';
import http, { request } from '../utils/fetch';

export async function addTemplate(data) {
  return http.upload(api.templateAdd, data);
}

export async function deleteTemplates({ templates }) {
  return await request(api.templateDelete, undefined,{ ids:templates.join(',') })
}

export async function updateTemplate(data) {
  return http.upload(api.templateUpdate, data);
}

export async function exportTemplate(id) {
  return request(api.templateExport,{id})
}

export async function copyTemplatePassword(query) {
  return request(api.copyTemplatePassword, undefined, query);
}

export async function postTemplatePassword(body, error) {
  return request(api.postTemplatePassword, undefined, undefined, JSON.stringify(body), {
    hideError: true,
    error,
  });
}

export async function importTemplate(
  { templateConfig, componentsConfig, panelsConfig, screenConfig, filtersConfig },
  signal,
) {
  return request(
    api.templateImport,
    undefined,
    undefined,
    JSON.stringify({
      templateConfig: JSON.parse(templateConfig),
      componentsConfig: JSON.parse(componentsConfig),
      panelsConfig: JSON.parse(panelsConfig),
      screenConfig: JSON.parse(screenConfig),
      filtersConfig: JSON.parse(filtersConfig),
    }),
    { signal },
  );
}
