import { assertString } from "../../../shared/src/asserts.js";
function getLikePredicate(pattern, flags) {
  const op = getLikeOp(String(pattern), flags);
  return (lhs) => {
    assertString(lhs);
    return op(String(lhs));
  };
}
function getLikeOp(pattern, flags) {
  if (!/_|%|\\/.test(pattern)) {
    if (flags === "i") {
      const rhsLower = pattern.toLowerCase();
      return (lhs) => lhs.toLowerCase() === rhsLower;
    }
    return (lhs) => lhs === pattern;
  }
  const re = patternToRegExp(pattern, flags);
  return (lhs) => re.test(lhs);
}
const specialCharsRe = /[$()*+.?[\]\\^{|}]/;
function patternToRegExp(source, flags = "") {
  let pattern = "^";
  for (let i = 0; i < source.length; i++) {
    let c = source[i];
    switch (c) {
      case "%":
        pattern += ".*";
        break;
      case "_":
        pattern += ".";
        break;
      // @ts-expect-error fallthrough
      case "\\":
        if (i === source.length - 1) {
          throw new Error("LIKE pattern must not end with escape character");
        }
        i++;
        c = source[i];
      // fall through
      default:
        if (specialCharsRe.test(c)) {
          pattern += "\\";
        }
        pattern += c;
        break;
    }
  }
  return new RegExp(pattern + "$", flags + "m");
}
export {
  getLikePredicate
};
//# sourceMappingURL=like.js.map
