/* eslint-disable import/no-duplicates */
/*
 * Copyright (c) 2016-present Invertase Limited & Contributors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this library 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 { ReactNativeFirebaseModule } from '@react-native-firebase/app-types/index.js.flow';

export interface Statics {}

/**
 * An interface representing the language identification options to be used with the
 * `identifyLanguage` and `identifyPossibleLanguages` methods.
 */
export interface LanguageIdentificationOptions {
  /**
   * The confidence threshold for language identification. The identified languages will have a
   * confidence higher or equal to the confidence threshold. The value should be between 0 and 1, e.g. 0.5.
   *
   * If no value is set, a default value is used instead.
   *
   */
  confidenceThreshold?: number;
}

/**
 * An identified language for the given input text. Returned as an Array of IdentifiedLanguage from
 * `identifyPossibleLanguages`.
 */
export interface IdentifiedLanguage {
  /**
   * The [BCP-47 language code](https://en.wikipedia.org/wiki/IETF_language_tag) for the language, e.g. 'en'.
   */
  language: string;

  /**
   * The confidence score of the language. A float value between 0 and 1.
   */
  confidence: number;
}

/**
 * An interface representing a suggest reply, an array of these are returned from `suggestReplies`
 */
export interface SuggestedReply {
  /**
   * The smart reply text.
   */
  text: string;
}

/**
 * A `TextMessage` interface provided to `suggestReplies()`.
 */
export interface TextMessage {
  /**
   * The message text.
   *
   * This is required and must not be an empty string.
   */
  text: string;

  /**
   * Whether the message is a local user. If false, a `userId` must be provided for the message.
   *
   * Defaults to true.
   */
  isLocalUser?: boolean;

  /**
   * A user ID of a remote user.
   *
   * Used to help better identify users to provide more accurate replies.
   */
  userId?: string;

  /**
   * The timestamp of the message in milliseconds.
   *
   * Defaults to now (`Date.now()`).
   */
  timestamp?: number;
}

/**
 * The Firebase ML Kit service interface.
 *
 * > This module is available for the default app only.
 *
 * #### Example
 *
 * Get the ML Kit service for the default app:
 *
 * ```js
 * const defaultAppMLKit = firebase.naturalLanguage();
 * ```
 */
export interface Module extends ReactNativeFirebaseModule {
  /**
   * Identifies the main language for the given text.
   *
   * Returns a promise that resolves with a [BCP-47 language code](https://en.wikipedia.org/wiki/IETF_language_tag) of the detected language.
   *
   * If the language was undetected or unknown the code returned is `und`.
   *
   * #### Example
   *
   * ```js
   * const language = await firebase.naturalLanguage().identifyLanguage('Hello there. General Kenobi.');
   * console.warn(language); // en
   *
   * const unknownLanguage = await firebase.naturalLanguage().identifyLanguage('foo bar baz', { confidenceThreshold: 0.9 });
   * console.warn(language); // und
   * ```
   *
   * @param text The input text to use for identifying the language. Inputs longer than 200 characters are truncated to 200 characters, as longer input does not improve the detection accuracy.
   * @param options See `LanguageIdentificationOptions`.
   */
  identifyLanguage(text: string, options?: LanguageIdentificationOptions): Promise<string>;

  /**
   * Identifies possible languages for the given text.
   *
   * #### Example
   *
   * ```js
   * const identifiedLanguages = firebase.naturalLanguage().identifyPossibleLanguages('hello world');
   * console.warn(identifiedLanguages[0].language); // en
   * ```
   *
   * @param text The input text to use for identifying the language. Inputs longer than 200 characters are truncated to 200 characters, as longer input does not improve the detection accuracy.
   * @param options See `LanguageIdentificationOptions`.
   */
  identifyPossibleLanguages(
    text: string,
    options?: LanguageIdentificationOptions,
  ): Promise<IdentifiedLanguage[]>;

  /**
   * Returns suggested replies for a conversation.
   *
   * #### Example
   *
   * ```js
   * const replies = await firebase.naturalLanguage().suggestReplies([
   *   { text: "Hey, long time no speak!", },
   *   { text: 'I know right, it has been a while..', userId: 'xxxx', isLocalUser: false },
   *   { text: 'We should catch up sometime' },
   *   { text: 'Definitely, how about we go for lunch this week?', userId: 'xxxx', isLocalUser: false },
   * ]);
   * ```
   *
   * @param messages An array of `TextMessage` interfaces.
   */
  suggestReplies(messages: TextMessage[]): Promise<SuggestedReply[]>;
}

declare module '@react-native-firebase/ml-natural-language' {
  import type {
    ReactNativeFirebaseNamespace,
    ReactNativeFirebaseModuleAndStatics,
  } from '@react-native-firebase/app-types/index.js.flow';
  /**
   * @example
   * ```js
   * import { firebase } from '@react-native-firebase/ml-natural-language';
   * firebase.naturalLanguage().X(...);
   * ```
   */
  declare export var firebase: {} & ReactNativeFirebaseNamespace;

  /**
   * @example
   * ```js
   * import naturalLanguage from '@react-native-firebase/ml-natural-language';
   * naturalLanguage().X(...);
   * ```
   */
  declare export default ReactNativeFirebaseModuleAndStatics<Module, Statics>;
}

/**
 * Attach namespace to `firebase.` and `FirebaseApp.`.
 */
declare module '@react-native-firebase/app-types' {
  import type { ReactNativeFirebaseModuleAndStatics } from '@react-native-firebase/app-types/index.js.flow';

  declare interface ReactNativeFirebaseNamespace {
    /**
     * ML Kit Natural Language
     */
    naturalLanguage: ReactNativeFirebaseModuleAndStatics<Module, Statics>;
  }

  declare interface FirebaseApp {
    /**
     * ML Kit Natural Language
     */
    naturalLanguage(): Module;
  }
}
