"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var loan_exports = {};
__export(loan_exports, {
  Loan: () => Loan
});
module.exports = __toCommonJS(loan_exports);
var import_pui_app_bridge = require("@elliemae/pui-app-bridge");
class Loan extends import_pui_app_bridge.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 import_pui_app_bridge.Event({
    name: "applicationselected",
    objectId: "loan"
  });
  Close = new import_pui_app_bridge.Event({
    name: "close",
    objectId: "loan"
  });
  Change = new import_pui_app_bridge.Event({
    name: "change",
    objectId: "loan"
  });
  Committed = new import_pui_app_bridge.Event({
    name: "committed",
    objectId: "loan"
  });
  EditModeChange = new import_pui_app_bridge.Event({
    name: "editmodechange",
    objectId: "loan"
  });
  FieldChanged = new import_pui_app_bridge.Event({
    name: "fieldchanged",
    objectId: "loan"
  });
  MilestoneCompleted = new import_pui_app_bridge.Event({
    name: "milestonecompleted",
    objectId: "loan"
  });
  Open = new import_pui_app_bridge.Event({
    name: "open",
    objectId: "loan"
  });
  PreCommit = new import_pui_app_bridge.Event({
    name: "precommit",
    objectId: "loan"
  });
  PreMilestoneComplete = new import_pui_app_bridge.Event({
    name: "premilestonecomplete",
    objectId: "loan"
  });
  Sync = new import_pui_app_bridge.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();
  };
}
