// ========= 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 { IOResult } from '../../../../io/types.ts'
import type { PathSpec } from '../../../../types.ts'
import { mvGeneric } from '../../generic/mv.ts'
import type { Builder } from '../adapter.ts'

export const MV_BUILDER: Builder = {
  name: 'mv',
  write: true,
  fn: (ops, accessor, paths, _texts, opts) => {
    if (paths.length < 2) {
      return Promise.resolve([
        null,
        new IOResult({ exitCode: 1, stderr: new TextEncoder().encode('mv: missing operand\n') }),
      ])
    }
    const { rename } = ops
    if (rename === undefined) {
      throw new Error('mv: backend provides no rename op')
    }
    return mvGeneric(
      paths,
      (p: PathSpec) => ops.stat(accessor, p, opts.index ?? undefined),
      { rename: (src: PathSpec, target: PathSpec) => rename(accessor, src, target) },
      opts.flags.n === true,
      opts.flags.v === true,
      opts.index ?? undefined,
    )
  },
}
