import{DataType as t,NumberUtils as r,DateUtils as i,UserInterface as e}from"@sankhyalabs/core";import{F as a}from"./p-ff1990ad.js";import{F as n}from"./p-84345e7a.js";class l{static buildFilterExpression(t){var r;if(null==t)return;let i=null!==(r=this.buildVariableParameterValue(t))&&void 0!==r?r:void 0,e=t.expression||"",a="'";switch(t.paramVariable&&(a=""),t.operand){case"IN":e+=` ${n[t.operand]} (${i})`;break;case"NULL":case"NOT_NULL":e+=` IS ${n[t.operand]}`;break;case"LIKE":e+=` LIKE '%${i}%'`;break;case"START_WITH":e+=` LIKE '${i}%'`;break;case"FINISHED_WITH":e+=` LIKE '%${i}'`;break;default:t.operand&&(e+=` ${n[t.operand]}`),["string","number"].includes(typeof i)&&(i=isNaN(i)?`${a}${i}${a}`:i,e+=` ${i}`)}return e}static buildVariableParameterValue(i){var a;if(null!=i){if(i.paramVariable)return this.proccessVarialbeParameter(i);if(i.operand!==n.NULL&&"NOT_NULL"!==i.operand){let n=i.value;const l=i.type;if(e.SEARCH!==i.userInterface&&i.userInterface!==e.OPTIONSELECTOR&&t.OBJECT!=l||(n=null!==(a=null==n?void 0:n.value)&&void 0!==a?a:n),t.NUMBER==l&&null!=n)n=r.stringToNumber(n),this.checkIsValidNumber(n);else if(t.DATE==l){if(n=this.convertDataValue(n),void 0===n)return}else t.BOOLEAN==l&&(n=n?"S":"N");return n}return i.operand}}static checkIsValidNumber(t){if(isNaN(t))throw new Error("O valor informado não é um número válido.")}static proccessVarialbeParameter(t){if(null!=t.systemConfig){const r=t.systemConfig;return`?:{entidade=${r.entity||""};campo=${r.fieldName||""}}`}if(null!=t.userConfig){const r=t.userConfig;return`?:{desc=${r.description||""};tipo=${r.type||""}}`}return`?:{entidade=${t.entityName||""};campo=${t.fieldName||""}}`}static convertDataValue(t){if(null!=t&&""!==t&&void 0!==t){if("string"==typeof t){const r=t.trim();if(""===r||"/"===r||"  /  /"===r)return;t=new Date(t)}if(!(t instanceof Date&&isNaN(t.getTime())))return i.formatDate(t)}}static buildGroupExpression(t){var r,i;if(!(null===(r=null==t?void 0:t.items)||void 0===r?void 0:r.length))return"";const e=t.items.map((t=>this.buildFilterExpression(t))).join(` ${t.operand} `);if((null===(i=null==t?void 0:t.childrenGroups)||void 0===i?void 0:i.length)>0){const r=t.childrenGroups.map((t=>this.buildGroupExpression(t))).join(` ${t.operand} `);return`(${e} ${t.operand} (${r}))`}return`(${e})`}static validateFields(t){const r=this.buildPlainExpressionItems(t);for(const t of r)if(!this.validateExpressionItem(t))return!1;return!0}static buildPlainExpressionItems(t){const{items:r=[],childrenGroups:i=[]}=t||{},e=t=>t.reduce(((t,r)=>{var i;return r.items&&(t=t.concat(r.items)),(null===(i=r.childrenGroups)||void 0===i?void 0:i.length)&&(t=t.concat(e(r.childrenGroups))),t}),[]);return r.concat(e(i))}static validateExpressionItem(t){return Object.entries({fieldName:t=>!!t,value:(t,r)=>!!r||null!=t,operand:t=>!!t}).every((r=>{const[i,e]=r,a=t.paramVariable||"SWITCH"===t.userInterface||["NULL","NOT_NULL"].includes(t.operand);return e(t[i],a)}))}static getNormalizedValue(r,i){return r.reduce(((r,i,e)=>(i.dataType!==t.BOOLEAN||null!=r&&null!=r[e]||((r=r&&!Array.isArray(r)?[r]:r||[])[e]=!1),r)),i)}static validateVariableValues(r,i){if(!r)return!0;let e=i||r.value,n=!0;if(r.type===a.PERSONALIZED){const i=(r.groupedItems||[]).filter((t=>t.visible)).filter((t=>{var r,i,e;return!!(null===(e=null===(i=null===(r=t.props)||void 0===r?void 0:r.personalizedFilter)||void 0===i?void 0:i.parameters)||void 0===e?void 0:e.length)})),a=i.flatMap((t=>{var r;return null===(r=t.props.personalizedFilter)||void 0===r?void 0:r.parameters})),l=i.flatMap((r=>r.props.personalizedFilter.parameters.flatMap(((i,e)=>r.value?(void 0!==r.value[e]||i.dataType!==t.BOOLEAN)&&r.value[e]:null))));e=this.getNormalizedValue(a,e),n=a.length===l.length&&l.every((t=>null!=t))}return n}static buildVariableParameters(t){const r=this.buildPlainExpressionItems(t),i=[];for(const t of r)t.paramVariable&&i.push({entityName:t.entityName,fieldName:t.fieldName});return i}}export{l as P}