// ========= Copyright 2026 @ Strukto.AI All Rights Reserved. =========
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ========= Copyright 2026 @ Strukto.AI All Rights Reserved. =========

import { detectScope } from '../../../core/mongodb/scope.ts'
import { SEARCHERS } from '../../../core/mongodb/search.ts'
import { ResourceName } from '../../../types.ts'
import { command } from '../../config.ts'
import { specOf } from '../../spec/builtins.ts'
import { makeSearch } from '../generic_bind/search.ts'
import { pushdownOperand } from '../grep_helper.ts'
import { MONGODB_IO } from './io.ts'

// The $regex push-down prints each matching document as a whole line;
// pushdownOperand defers shaping flags and multi-operand lines to the
// generic scan, which streams documents rather than reading whole
// collections.
export const MONGODB_GREP = command({
  name: 'grep',
  resource: ResourceName.MONGODB,
  spec: specOf('grep'),
  fn: makeSearch('grep', detectScope, SEARCHERS, MONGODB_IO, {
    qualify: pushdownOperand,
    guard: true,
    stream: true,
  }),
})
