import * as React from 'react';
import { CommonAddress as Address } from '@wix/ambassador-wix-atlas-service-web/http';
import { AmbassadorHTTPError as AtlasError } from '@wix/ambassador/runtime/http';
import { AddressInputProps } from '../AddressInput';
import { DropdownLayoutValueOption as Option } from '../DropdownLayout';
import { AtlasInitOptions } from '../providers/useAtlasClient';

export { Option, Address, AtlasError };

export type GetAddress = () => Promise<Address>;

export interface AtlasAddressInputProps
  extends
    Omit<AddressInputProps, 'options' | 'onSelect' | 'onManuallyInput'>,
    AtlasInitOptions {
  /** Fetch predictions debounce in milliseconds (default: 200) */
  debounceMs?: number;
  /** Allows passing a custom debounce function (default: lodash debounce).
   * Usage:
   * (callback: Function, debounceMs: number) => Function */
  debounceFn?: (callback: Function, debounceMs: number) => Function;
  /** Sets the layout of `mainLabel` and `secondaryLabel`. The possible options can be either side by side or vertically stacked. */
  optionLayout?: 'single-line' | 'double-line';
  /** Pass any component to show as the prefix of the option, e.g., text, icon. */
  optionPrefix?: React.ReactNode;
  /** Pass any component to show as the suffix of the option, e.g., text, icon, button. */
  optionSuffix?: React.ReactNode;
  /** Defines a callback function which is called whenever a user selects a different option in the list.
   * @param {DropdownLayoutOption} option selected option
   * @param {() => Promise<Address>} getAddress function for retrieving additional place details
   */
  onSelect?: (option: Option, getAddress: GetAddress) => void;
  /** Defines a handler for prediction fetching errors. Returns an error object.
   * you can read these [guidelines](https://bo.wix.com/wix-docs/rnd/platformization-guidelines/errors#platformization-guidelines_errors_errors)
   * to learn about the meaning of each error status.
   */
  onError?: (error: AtlasError) => any;
  /** Specifies whether to trigger `onSelect` handler when performing a Submit-Action (Enter or Tab key down).
   * If set to true, `onSelect` will be called with the following params:
   *
   * `option`: an option with a label set to the input value.
   *
   * `getAddress`: function for retrieving additional place details
   *    uses Atlas's search function to return the closest result to the input value
   *
   * This is useful when looking for locations for which Atlas does not give suggestions - for example: Apartment/Apt.  */
  selectOnSubmit?: boolean;
  /** Sets the `language` to pass to the Atlas Service.
   * Use the language code e.g. `'en'`.
   */
  language?: string;
  /** Sets the `locale` to pass to the Atlas Service.
   * Locale consists of language code and country code separated by a dash e.g. `'en-us'`.
   */
  locale?: string;
}

export type AtlasAddressInputImperativeActions = {
  focus(): void;
};

declare const AtlasAddressInput: React.ForwardRefExoticComponent<
  AtlasAddressInputProps &
    React.RefAttributes<AtlasAddressInputImperativeActions>
>;

export default AtlasAddressInput;
