/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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.
 */


import type { ClientProvider } from '../../client/index.js';
import { createStream } from './create-stream.command.js';
import { getStream } from './get-stream.command.js';


/**
 * Creates a virtual command that ensures a stream exists.
 * If the stream does not exist, it will be created.
 *
 * @param c - Client provider function
 * @returns Function that ensures a stream exists by name
 */
export const ensureStream = (c: ClientProvider) =>
  async function ensureStream(streamName: string) {
    const stream = await getStream(c)({ streamId: streamName });
    return stream === null ?
      createStream(c)({ name: streamName }) : stream;
  };
