import { ApplicationContext, ErrorException, StringUtils } from "@sankhyalabs/core";
export default class LaunchScreenExecutor {
  constructor() {
    this._application = ApplicationContext.getContextValue("__SNK__APPLICATION__");
  }
  async execute(action, dataUnit) {
    const resourceID = action.resourceID;
    if (!resourceID)
      return;
    let launchObject = await this.buildLaunchObject(action, dataUnit);
    this._application.openApp(resourceID, launchObject);
    return null;
  }
  buildLaunchObject(action, dataUnit) {
    return new Promise((resolve) => {
      let params = action.actionConfig.params.param;
      if (params && params.length > 0) {
        let launchObject = {};
        let processedParams = [];
        params.forEach(param => {
          const localField = param.localField;
          let value = dataUnit.getFieldValue(localField);
          if (!value) {
            let localFieldLabel = dataUnit.getField(localField).label;
            localFieldLabel = StringUtils.isEmpty(dataUnit.getField(localField).label) ? localField : localFieldLabel;
            throw new ErrorException(this._application.messagesBuilder.getMessage("snkActionsButton.title.error", undefined), this._application.messagesBuilder.getMessage("snkActionsButton.launchScreen.emptyField", { description: action.description, localFieldLabel: localFieldLabel }));
          }
          if (value !== null && typeof value === 'object' && 'value' in value) {
            value = value.value;
          }
          const finalValue = StringUtils.isEmpty(value === null || value === void 0 ? void 0 : value.toString()) ? undefined : value.toString();
          launchObject[param.targetField] = finalValue;
          processedParams.push({
            fieldName: param.targetField,
            value: finalValue
          });
        });
        launchObject.ACTION_PARAMETERS = processedParams;
        launchObject.call_time = Date.now();
        resolve(launchObject);
      }
      resolve(null);
    });
  }
}
