import { ScriptingObject, Event } from "@elliemae/pui-app-bridge";
class Loan extends ScriptingObject {
  #loanData;
  #appBridge;
  constructor(appBridge) {
    super("loan");
    this.#appBridge = appBridge;
    this.#loanData = {
      id: "1234567890",
      status: "active",
      loanAmount: 1e5,
      interestRate: 3.5,
      term: 30
    };
    window.__TEST__ = window.__TEST__ ?? {};
    window.__TEST__.soLogs = window.__TEST__.soLogs ?? [];
  }
  #log(message) {
    window.__TEST__.soLogs.push(message);
  }
  ApplicationSelected = new Event({
    name: "applicationselected",
    objectId: "loan"
  });
  Close = new Event({
    name: "close",
    objectId: "loan"
  });
  Change = new Event({
    name: "change",
    objectId: "loan"
  });
  Committed = new Event({
    name: "committed",
    objectId: "loan"
  });
  EditModeChange = new Event({
    name: "editmodechange",
    objectId: "loan"
  });
  FieldChanged = new Event({
    name: "fieldchanged",
    objectId: "loan"
  });
  MilestoneCompleted = new Event({
    name: "milestonecompleted",
    objectId: "loan"
  });
  Open = new Event({
    name: "open",
    objectId: "loan"
  });
  PreCommit = new Event({
    name: "precommit",
    objectId: "loan"
  });
  PreMilestoneComplete = new Event({
    name: "premilestonecomplete",
    objectId: "loan"
  });
  Sync = new Event({
    name: "sync",
    objectId: "loan"
  });
  all = () => {
    this.#log("loan.all");
    return Promise.resolve(this.#loanData);
  };
  applyLock = (fieldId, lock) => {
    this.#log(`loan.applyLock: fieldId: ${fieldId}, lock: ${String(lock)}`);
    return Promise.resolve();
  };
  calculate = () => {
    this.#log("loan.calculate");
    return Promise.resolve(this.#loanData);
  };
  commit = async () => {
    this.#log("loan.commit");
    const feedBack = await this.#appBridge.dispatchEvent({
      event: this.PreCommit,
      eventParams: {
        id: this.#loanData.id
      },
      eventOptions: {
        timeout: 1e3
      }
    });
    this.#log(`loan.precommit:event:feedback: ${JSON.stringify(feedBack)}`);
    if (feedBack.every((item) => item === true)) {
      this.#log("loan.commit:succeed");
    } else {
      this.#log("loan.commit:failed");
    }
    return this.#loanData.id;
  };
  execAction = (type, params) => {
    this.#log(
      `loan.execAction: type: ${type}, params: ${JSON.stringify(params)}`
    );
    return Promise.resolve({
      status: "success",
      message: "Action executed successfully"
    });
  };
  getCollection = (name) => {
    this.#log(`loan.getCollection: name: ${name}`);
    return Promise.resolve({
      name,
      data: [],
      id: "collection-id",
      objectType: "LoanCollection",
      removeAt: (index) => {
        this.#log(`loan.getCollection.removeAt: index: ${index}`);
      }
    });
  };
  getCurrentApplication = () => {
    this.#log("loan.getCurrentApplication");
    return Promise.resolve({
      ...this.#loanData,
      index: 0,
      legacyId: "legacy-id"
    });
  };
  getField = (id) => {
    this.#log(`loan.getField: id: ${id}`);
    return Promise.resolve(this.#loanData.name);
  };
  getFieldOptions = (param) => {
    this.#log(`loan.getFieldOptions: param: ${JSON.stringify(param)}`);
    return Promise.resolve({
      fieldId: [
        {
          id: "option1",
          label: "Option 1",
          name: "OptionName1",
          value: "OptionValue1"
        },
        {
          id: "option2",
          label: "Option 2",
          name: "OptionName2",
          value: "OptionValue2"
        }
      ]
    });
  };
  getFields = (ids) => {
    this.#log(`loan.getFields: ids: ${JSON.stringify(ids)}`);
    const fields = ids.reduce((acc, id) => {
      acc[id] = this.#loanData.name;
      return acc;
    }, {});
    return Promise.resolve(fields);
  };
  isReadOnly = () => {
    this.#log("loan.isReadOnly");
    return Promise.resolve(false);
  };
  merge = () => {
    this.#log("loan.merge");
    return Promise.resolve();
  };
  setCurrentApplication = (options) => {
    this.#log(
      `loan.setCurrentApplication: options: ${JSON.stringify(options)}`
    );
    return Promise.resolve();
  };
  setEditMode = (options) => {
    this.#log(`loan.setEditMode: options: ${JSON.stringify(options)}`);
    return Promise.resolve();
  };
  setFields = (fields) => {
    this.#log(`loan.setFields: fields: ${JSON.stringify(fields)}`);
    Object.keys(fields).forEach((key) => {
      const field = fields[key];
    });
    return Promise.resolve();
  };
}
export {
  Loan
};
