(function (global) {
  global.ARQ = global.ARQ || {};
  ARQ.projects = {
    list: function () {
      return ARQ.storage.apiJson('arquitectListProjects');
    },
    create: function (payload) {
      return ARQ.storage.apiJson('arquitectCreateProject', { method: 'POST', body: payload });
    },
    load: function (projectId) {
      return ARQ.storage.apiJson('arquitectLoadProject', { query: { projectId: projectId } });
    },
    save: function (project) {
      return ARQ.storage.apiJson('arquitectSaveProject', { method: 'POST', body: { project: project } });
    },
    remove: function (projectId) {
      return ARQ.storage.apiJson('arquitectDeleteProject', { method: 'POST', body: { projectId: projectId } });
    },
    duplicate: function (projectId, nombre) {
      return ARQ.storage.apiJson('arquitectDuplicateProject', {
        method: 'POST',
        body: { projectId: projectId, nombre: nombre || '' },
      });
    },
    rename: function (projectId, nombre) {
      return ARQ.storage.apiJson('arquitectRenameProject', {
        method: 'POST',
        body: { projectId: projectId, nombre: nombre },
      });
    },
    uploadFile: function (projectId, folder, file) {
      var fd = new FormData();
      fd.append('projectId', projectId);
      fd.append('folder', folder || 'assets');
      fd.append('file', file);
      return ARQ.storage.apiForm('arquitectUploadProjectFile', fd);
    },
    saveSnapshot: function (projectId, label, data) {
      return ARQ.storage.apiJson('arquitectSaveSnapshot', {
        method: 'POST',
        body: { projectId: projectId, label: label || 'snap', data: data },
      });
    },
    openaiKeyStatus: function () {
      return ARQ.storage.apiJson('arquitectOpenaiKeyStatus');
    },
    saveOpenaiKey: function (apiKey) {
      return ARQ.storage.apiJson('arquitectSaveOpenaiKey', { method: 'POST', body: { apiKey: apiKey || '' } });
    },
    openaiImage: function (body) {
      return ARQ.storage.apiJson('arquitectOpenaiImage', { method: 'POST', body: body });
    },
    openaiImageFromImage: function (body) {
      return ARQ.storage.apiJson('arquitectOpenaiImageFromImage', { method: 'POST', body: body });
    },
  };
})(typeof window !== 'undefined' ? window : this);
