const log_error = function (SESSION_ID, action, descP, detailsP, warning, prog_id) {
  func.utils.debug.log(SESSION_ID, log_nodeId, {
    module: 'event',
    action,
    source: '',
    prop: descP,
    details: detailsP,
    result: null,
    error: !warning,
    warning: warning,
    fields: null,
    type: 'event',
    prog_id,
  });
};

const get_ui_ids = function (SESSION_ID, event_prop) {
  if (event_prop.selector === '@_THIS') event_prop.selector = elementP;
  // var ui_ids = [];
  if (event_prop.selector_type == 'tree_id') {
    let $elm = func.UI.utils.find_in_element_data('xuData', $(SESSION_OBJ[SESSION_ID].root_element), 'ui_id', event_prop.selector);
    return [
      {
        ui_id: event_prop.selector,
        tagName: $elm?.data()?.xuData?.node.tagName, //$elm[0]?.tagName?.toLowerCase(),
      },
    ];
  }

  return _.map($(SESSION_OBJ[SESSION_ID].root_element).find(event_prop.selector), function (val, key) {
    return {
      ui_id: $(val)?.data()?.xuData?.ui_id,
      tagName: $(val)?.data()?.xuData?.node.tagName, //$(val)[0]?.tagName?.toLowerCase(),
    };
  });
};

const get_ui_id = async function (SESSION_ID, dsSession, event_prop, rowP) {
  if (event_prop.selector === '@_THIS') event_prop.selector = elementP;
  return await func.expression.get(SESSION_ID, event_prop.selector, dsSession, 'ui_id', rowP);
};
const set_info_value = async function (SESSION_ID, value, source, event_prop, resolve) {
  var _session = SESSION_OBJ[SESSION_ID];
  if (!event_prop.field_return_value) {
    log_error(SESSION_ID, source, ' Field cannot be empty');
    return;
  }
  var fieldId = event_prop.field_return_value.substr(1);
  if (event_prop.field_return_value.substr(0, 1) !== '@') {
    log_error(SESSION_ID, source, fieldId + ' Field not valid');
    return;
  }
  var fieldsChanged_arr = [];
  var rows_changed_arr = [];
  var returned_ds;
  const update_ds = async function (dsP, rowIdP) {
    if (!dsP || !_session.DS_GLB[dsP]) return;
    var _ds = _session.DS_GLB[dsP];

    try {
      const row_idx = func.common.find_ROWID_idx(_ds, rowIdP);

      if (_ds.data_feed?.rows?.[row_idx]?.[fieldId]) _ds.data_feed.rows[row_idx][fieldId] = value;
    } catch (err) {
      console.error(err);
    }

    if (!fieldsChanged_arr.includes(fieldId)) fieldsChanged_arr.push(fieldId);
    if (!rows_changed_arr.includes(rowIdP)) rows_changed_arr.push(rowIdP);
    returned_ds = dsP;
    const _view_obj = await func.utils.VIEWS_OBJ.get(SESSION_ID, _ds.prog_id);
    var field_row_id = func.common.find_item_by_key(_view_obj.progFields, 'field_id', fieldId)?.id;

    func.utils.debug.watch(SESSION_ID, _ds.prog_id + '%' + field_row_id, 'view_field', 'Source: ' + source, value);
  };
  const ret = await func.datasource.get_value(
    SESSION_ID,
    fieldId,
    dsSession,

    _session.DS_GLB[dsSession].currentRecordId,
  );
  await update_ds(ret.dsSessionP, ret.currentRecordId);
  if (returned_ds) {
    var obj = {
      sessionChanged: returned_ds,
      viewType_target: _session.DS_GLB[returned_ds].viewType,
      rows_changed: rows_changed_arr,
      fieldsChanged: fieldsChanged_arr,
    };
    await func.datasource.update(SESSION_ID, obj);
    resolve();
  }
};

const element_not_found = function (SESSION_ID, dsP, jobNoP, alter_attr, ui_id) {
  var _session = SESSION_OBJ[SESSION_ID];
  if (_.isEmpty(SCREEN_BLOCKER_OBJ)) {
    // still busy not require alert
    if (_session.DS_GLB[dsP]) {
      log_error(SESSION_ID, 'UI element not found', alter_attr + ' > ' + ui_id, null, _session.DS_GLB[dsP].prog_id);
    }
  }
  if (jobNoP) {
    func.events.delete_job(SESSION_ID, jobNoP);
  }
};
