import { SaveErrorsEnum } from "./interfaces";
import { DataFetcher } from "../../DataFetcher";
const SERVICE = {
  save: "Attach.save",
  delete: "Attach.remove",
  view: "Attach.view",
  repository: "RepositorioArquivoSP.abreArquivo"
};
export class AttachFetcher {
  constructor(dataUnitName) {
    this.dataUnitName = dataUnitName;
  }
  async save({ dataUnit: dataUnitName, record, updatingFields: fields, operation }) {
    var _a, _b;
    const isInsert = operation == "INSERT";
    const dataBody = isInsert ? fields : record;
    if (!isInsert && (fields === null || fields === void 0 ? void 0 : fields.DESCRICAO) && (fields === null || fields === void 0 ? void 0 : fields.DESCRICAO) !== (record === null || record === void 0 ? void 0 : record.DESCRICAO)) {
      throw new Error(SaveErrorsEnum.DESCRIPTION_CANNOT_BE_CHANGED);
    }
    const reqBody = {
      "anexo": {
        "codata": isInsert ? fields === null || fields === void 0 ? void 0 : fields.REGISTER_KEY : record === null || record === void 0 ? void 0 : record.CODATA,
        "sequencia": (dataBody === null || dataBody === void 0 ? void 0 : dataBody.SEQUENCIA) || "0",
        "tipo": (dataBody === null || dataBody === void 0 ? void 0 : dataBody.TIPO) || "N",
        "descricao": dataBody === null || dataBody === void 0 ? void 0 : dataBody.DESCRICAO,
        "arquivo": (_b = (_a = fields.CAMINHO_ARQUIVO) === null || _a === void 0 ? void 0 : _a[0]) === null || _b === void 0 ? void 0 : _b.name,
        "ehInclusao": isInsert ? "S" : "N",
        "ehArquivoRepositorio": "N"
      }
    };
    return new Promise((resolve, reject) => {
      DataFetcher.get().callServiceBroker(SERVICE.save, JSON.stringify(reqBody))
        .then(result => {
        var _a;
        return resolve([Object.assign(Object.assign(Object.assign({}, result), fields), { __owner__dataunit__name__: (_a = this.dataUnitName) !== null && _a !== void 0 ? _a : dataUnitName })]);
      })
        .catch(error => reject(error));
    });
  }
  async edit(change) {
    throw new Error("Method not implemented.");
  }
  async delete(record) {
    var request = {
      anexo: {
        codata: record.CODATA,
        tipo: record.TIPO,
        descricao: record.DESCRICAO,
      }
    };
    const result = await DataFetcher.get().callServiceBroker(SERVICE.delete, JSON.stringify(request));
    return Promise.resolve(result);
  }
  async getDownloadKey(record) {
    var _a, _b, _c;
    if (record.EHARQUIVOREPOSITORIO == 'S') {
      const request = {
        config: {
          path: record.ARQUIVO,
          tipoconteudo: record.TIPOCONTEUDO
        }
      };
      const result = await DataFetcher.get().callServiceBroker(SERVICE.repository, JSON.stringify(request));
      return Promise.resolve({
        chave: {
          valor: (_b = (_a = result === null || result === void 0 ? void 0 : result.responseBody) === null || _a === void 0 ? void 0 : _a.chave) === null || _b === void 0 ? void 0 : _b.valor
        }
      });
    }
    const criteria = {
      anexo: {
        codata: record.CODATA,
        codemp: record.CODEMP,
        sequencia: record.SEQUENCIA,
        tipo: record.TIPO,
        descricao: record.DESCRICAO,
        tipoConteudo: record.TIPOCONTEUDO
      }
    };
    const result = await DataFetcher.get().callServiceBroker(SERVICE.view, JSON.stringify(criteria));
    if (result === null || result === void 0 ? void 0 : result.chaveAnexo) {
      return Promise.resolve({
        chave: {
          valor: (_c = result === null || result === void 0 ? void 0 : result.chaveAnexo) === null || _c === void 0 ? void 0 : _c.idChaveAnexo
        }
      });
    }
    return Promise.reject(new Error("File not found."));
  }
}
