var __defProp = Object.defineProperty;
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });

// src/queries/search.ts
async function parseSearchField(engine, key, fieldData, inputFieldParser, translatedModelInstance, result) {
  if (typeof fieldData === "object" && fieldData !== null) {
    if (typeof fieldData.like === "object") {
      if (fieldData.like?.ignoreCase) {
        await engine.query.search.parseSearchFieldValue("like", key, translatedModelInstance, await inputFieldParser(fieldData.like?.ignoreCase), result, {
          ignoreCase: true
        });
      } else if ("not" in fieldData.like) {
        if (typeof fieldData.like.not === "object") {
          await engine.query.search.parseSearchFieldValue("like", key, translatedModelInstance, await inputFieldParser(fieldData.like.not.ignoreCase), result, {
            isNot: true,
            ignoreCase: true
          });
        } else {
          await engine.query.search.parseSearchFieldValue("like", key, translatedModelInstance, await inputFieldParser(fieldData.like.not), result, {
            isNot: true
          });
        }
      }
    }
    if (fieldData.like !== void 0) {
      await engine.query.search.parseSearchFieldValue("like", key, translatedModelInstance, await inputFieldParser(fieldData.like), result);
    }
    if (fieldData.and !== void 0) {
      const isArrayAndAtLeastOneElement = Array.isArray(fieldData.and) && fieldData.and.length > 1;
      if (isArrayAndAtLeastOneElement) {
        await engine.query.search.parseSearchFieldValue("and", key, translatedModelInstance, await inputFieldParser(fieldData.and), result);
      } else return {
        isValid: false,
        code: "invalid_in",
        reason: `The field '${key}' must contain at least two elements when using the 'and' clause`
      };
    }
    if (fieldData.or !== void 0) {
      const isArrayAndAtLeastTwoElements = Array.isArray(fieldData.or) && fieldData.or.length > 1;
      if (isArrayAndAtLeastTwoElements) {
        await engine.query.search.parseSearchFieldValue("or", key, translatedModelInstance, await inputFieldParser(fieldData.or), result);
      } else return {
        isValid: false,
        code: "invalid_in",
        reason: `The field '${key}' must contain at least two elements when using the 'or' clause`
      };
    }
    if (fieldData.in !== void 0) {
      const isInArrayAndAtLeastOneElement = Array.isArray(fieldData.in) && fieldData.in.length > 0 || Array.isArray(fieldData.in?.not) && (fieldData.in?.not || []).length > 0;
      const isInNotArrayAndAtLeastOneElement = Array.isArray(fieldData.in?.not) && (fieldData.in?.not || []).length > 0;
      if (isInArrayAndAtLeastOneElement) {
        await engine.query.search.parseSearchFieldValue(
          "in",
          key,
          translatedModelInstance,
          // eslint-disable-next-line ts/no-unnecessary-condition
          await Promise.all((fieldData.in || []).map((inValue) => inputFieldParser(inValue))),
          result
        );
      } else if (isInNotArrayAndAtLeastOneElement) {
        await engine.query.search.parseSearchFieldValue(
          "in",
          key,
          translatedModelInstance,
          // eslint-disable-next-line ts/no-unnecessary-condition
          await Promise.all((fieldData.in?.not || []).map((inValue) => inputFieldParser(inValue))),
          result,
          {
            isNot: true
          }
        );
      } else return {
        isValid: false,
        code: "invalid_in",
        reason: `The field '${key}' must contain at least one element when using the 'in' clause`
      };
    }
    if (fieldData.between !== void 0) {
      const isBetweenAnArrayAndExactlyTwoElements = (
        // eslint-disable-next-line ts/no-unnecessary-condition
        Array.isArray(fieldData.between) && fieldData.between.length === 2
      );
      const isBetweenNotAnArrayAndExactlyTwoElements = Array.isArray(fieldData.between?.not) && (fieldData.between?.not || []).length === 2;
      if (isBetweenAnArrayAndExactlyTwoElements) {
        await engine.query.search.parseSearchFieldValue(
          "between",
          key,
          translatedModelInstance,
          // eslint-disable-next-line ts/no-unnecessary-condition
          await Promise.all((fieldData.between || []).map((betweenValue) => inputFieldParser(betweenValue))),
          result
        );
      } else if (isBetweenNotAnArrayAndExactlyTwoElements) {
        await engine.query.search.parseSearchFieldValue(
          "between",
          key,
          translatedModelInstance,
          // eslint-disable-next-line ts/no-unnecessary-condition
          await Promise.all((fieldData.between?.not || []).map((betweenValue) => inputFieldParser(betweenValue))),
          result,
          {
            isNot: true
          }
        );
      } else return {
        isValid: false,
        code: "invalid_between",
        reason: `The field '${key}' must have exactly two elements when using the 'between' clause`
      };
    }
    if (fieldData.is && fieldData.is.not !== void 0) {
      await engine.query.search.parseSearchFieldValue("is", key, translatedModelInstance, await inputFieldParser(fieldData.is.not), result, {
        isNot: true
      });
    } else if (fieldData.is !== void 0) await engine.query.search.parseSearchFieldValue("is", key, translatedModelInstance, await inputFieldParser(fieldData.is), result);
    if (fieldData.greaterThan && fieldData.greaterThan.equal !== void 0) {
      await engine.query.search.parseSearchFieldValue("greaterThan", key, translatedModelInstance, await inputFieldParser(fieldData.greaterThan.equal), result, {
        equals: true
      });
    } else if (fieldData.greaterThan !== void 0) await engine.query.search.parseSearchFieldValue("greaterThan", key, translatedModelInstance, await inputFieldParser(fieldData.greaterThan), result);
    if (fieldData.lessThan && fieldData.lessThan.equal !== void 0) {
      await engine.query.search.parseSearchFieldValue("lessThan", key, translatedModelInstance, await inputFieldParser(fieldData.lessThan), result, {
        equals: true
      });
    } else if (fieldData.lessThan !== void 0) await engine.query.search.parseSearchFieldValue("lessThan", key, translatedModelInstance, await inputFieldParser(fieldData.lessThan.equal), result);
    return;
  }
  return await engine.query.search.parseSearchFieldValue("eq", key, translatedModelInstance, await inputFieldParser(fieldData), result);
}
__name(parseSearchField, "parseSearchField");
export {
  parseSearchField
};
