import{DataType as t,NumberUtils as i,DateUtils as r,UserInterface as e}from"@sankhyalabs/core";import{F as n}from"./p-ff1990ad.js";var a,l,s,o,u,c;!function(t){t.AND="AND",t.OR="OR"}(a||(a={})),function(t){t.SYSTEM_CONFIG="S",t.USER_CONFIG="E"}(l||(l={})),function(t){t.DATE="D",t.HORA="H",t.TEXT="S",t.INTEGER="I",t.DECIMAL="F",t.NUMBER="NUMBER"}(s||(s={})),function(t){t.AND="AND",t.OR="OR",t.IN="IN",t.DIFFERENT="<>",t.LIKE="LIKE",t.START_WITH="LIKE_START",t.FINISHED_WITH="LIKE_END",t.NULL="NULL",t.NOT_NULL="NOT NULL",t.EQUAL="=",t.LARGER=">",t.LARGER_EQUAL=">=",t.SMALLER="<",t.SMALLER_EQUAL="<="}(o||(o={})),function(t){t.FIELD="Campo",t.INSTANCE="Instancia"}(u||(u={})),function(t){t.name="Filtro padrão",t.id="DEFAULT_FILTER"}(c||(c={}));class d{static buildFilterExpression(t){var i;if(null==t)return;let r=null!==(i=this.buildVariableParameterValue(t))&&void 0!==i?i:void 0,e=t.expression||"",n="'";switch(t.paramVariable&&(n=""),t.operand){case"IN":e+=` ${o[t.operand]} (${r})`;break;case"NULL":case"NOT_NULL":e+=` IS ${o[t.operand]}`;break;case"LIKE":e+=` LIKE '%${r}%'`;break;case"START_WITH":e+=` LIKE '${r}%'`;break;case"FINISHED_WITH":e+=` LIKE '%${r}'`;break;default:t.operand&&(e+=` ${o[t.operand]}`),["string","number"].includes(typeof r)&&(r=isNaN(r)?`${n}${r}${n}`:r,e+=` ${r}`)}return e}static buildVariableParameterValue(r){var n;if(null!=r){if(r.paramVariable)return this.proccessVarialbeParameter(r);if(r.operand!==o.NULL&&"NOT_NULL"!==r.operand){let a=r.value;const l=r.type;if(e.SEARCH!==r.userInterface&&r.userInterface!==e.OPTIONSELECTOR&&t.OBJECT!=l||(a=null!==(n=null==a?void 0:a.value)&&void 0!==n?n:a),t.NUMBER==l&&null!=a)a=i.stringToNumber(a),this.checkIsValidNumber(a);else if(t.DATE==l){if(a=this.convertDataValue(a),void 0===a)return}else t.BOOLEAN==l&&(a=a?"S":"N");return a}return r.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 i=t.systemConfig;return`?:{entidade=${i.entity||""};campo=${i.fieldName||""}}`}if(null!=t.userConfig){const i=t.userConfig;return`?:{desc=${i.description||""};tipo=${i.type||""}}`}return`?:{entidade=${t.entityName||""};campo=${t.fieldName||""}}`}static convertDataValue(t){if(null!=t&&""!==t&&void 0!==t){if("string"==typeof t){const i=t.trim();if(""===i||"/"===i||"  /  /"===i)return;t=new Date(t)}if(!(t instanceof Date&&isNaN(t.getTime())))return r.formatDate(t)}}static buildGroupExpression(t){var i,r;if(!(null===(i=null==t?void 0:t.items)||void 0===i?void 0:i.length))return"";const e=t.items.map((t=>this.buildFilterExpression(t))).join(` ${t.operand} `);if((null===(r=null==t?void 0:t.childrenGroups)||void 0===r?void 0:r.length)>0){const i=t.childrenGroups.map((t=>this.buildGroupExpression(t))).join(` ${t.operand} `);return`(${e} ${t.operand} (${i}))`}return`(${e})`}static validateFields(t){const i=this.buildPlainExpressionItems(t);for(const t of i)if(!this.validateExpressionItem(t))return!1;return!0}static buildPlainExpressionItems(t){const{items:i=[],childrenGroups:r=[]}=t||{},e=t=>t.reduce(((t,i)=>{var r;return i.items&&(t=t.concat(i.items)),(null===(r=i.childrenGroups)||void 0===r?void 0:r.length)&&(t=t.concat(e(i.childrenGroups))),t}),[]);return i.concat(e(r))}static validateExpressionItem(t){return Object.entries({fieldName:t=>!!t,value:(t,i)=>!!i||null!=t,operand:t=>!!t}).every((i=>{const[r,e]=i,n=t.paramVariable||"SWITCH"===t.userInterface||["NULL","NOT_NULL"].includes(t.operand);return e(t[r],n)}))}static getNormalizedValue(i,r){return i.reduce(((i,r,e)=>(r.dataType!==t.BOOLEAN||null!=i&&null!=i[e]||((i=i&&!Array.isArray(i)?[i]:i||[])[e]=!1),i)),r)}static validateVariableValues(i,r){if(!i)return!0;let e=r||i.value,a=!0;if(i.type===n.PERSONALIZED){const r=(i.groupedItems||[]).filter((t=>t.visible)).filter((t=>{var i,r,e;return!!(null===(e=null===(r=null===(i=t.props)||void 0===i?void 0:i.personalizedFilter)||void 0===r?void 0:r.parameters)||void 0===e?void 0:e.length)})),n=r.flatMap((t=>{var i;return null===(i=t.props.personalizedFilter)||void 0===i?void 0:i.parameters})),l=r.flatMap((i=>i.props.personalizedFilter.parameters.flatMap(((r,e)=>i.value?(void 0!==i.value[e]||r.dataType!==t.BOOLEAN)&&i.value[e]:null))));e=this.getNormalizedValue(n,e),a=n.length===l.length&&l.every((t=>null!=t))}return a}static buildVariableParameters(t){const i=this.buildPlainExpressionItems(t),r=[];for(const t of i)t.paramVariable&&r.push({entityName:t.entityName,fieldName:t.fieldName});return r}}export{c as D,o as F,d as P,a,l as b,u as c,s as d}