/*
 * Copyright (c) 2024 Huawei Device Co., Ltd. All rights reserved
 * Use of this source code is governed by a MIT license that can be
 * found in the LICENSE file.
 */

import type { HostComponent, ViewProps } from 'react-native';
import type { ImageSource } from 'react-native/Libraries/Image/ImageSource';
import { DirectEventHandler, WithDefault, Int32 } from 'react-native/Libraries/Types/CodegenTypes';
import codegenNativeComponent from 'react-native/Libraries/Utilities/codegenNativeComponent';
import {UnsafeMixed} from './utils';

type Action = Readonly<{
    title: string,
    icon?: ImageSource,
    show?: WithDefault<'always' | 'ifRoom' | 'never', 'ifRoom'>,
    showWithText?: boolean
}>;

type ToolbarAndroidChangeEvent = Readonly<{
    position: Int32
}>;

type ColorValue = null | string;

export interface ToolbarAndroidProps extends ViewProps {

    /**
     * Internal bridging event emitted by the HarmonyOS side when the navigation
     * icon or a menu action is clicked: `position` is -1 for the navigation
     * icon, otherwise the index of the action in the actions array. The JS
     * wrapper (`js/ToolbarAndroid.harmony.js`) dispatches it to the public
     * `onIconClicked` / `onActionSelected` callbacks. Not meant to be set
     * directly by library users.
     */
    onSelect: DirectEventHandler<ToolbarAndroidChangeEvent>,

    /**
     * Internal bridging field that carries the actions array to the native
     * side: the JS wrapper resolves each action icon via `resolveAssetSource`
     * and forwards the processed array as `nativeActions`, which is consumed by
     * the HarmonyOS implementation (`Toolbar.ets`). Not meant to be set
     * directly by library users.
     */
    nativeActions?: UnsafeMixed<Array<Action>>,
    /**
     * Sets possible actions on the toolbar as part of the action menu. These
     * are displayed as icons or text on the right side of the widget. If they
     * don't fit they are placed in an 'overflow' menu.
     *
     * Note: the native side consumes the icon-resolved `nativeActions`
     * forwarded by the JS wrapper; this prop is kept in the spec for interface
     * completeness.
     */
    actions?: UnsafeMixed<Array<Action>>,
    /**
     * Sets the toolbar logo.
     */
    logo?: ImageSource,
    /**
     * Sets the navigation icon.
     */
    navIcon?: ImageSource,
    /**
     * Callback that is called when an action is selected. The only argument that is passed to the
     * callback is the position of the action in the actions array.
     */
    onActionSelected?: DirectEventHandler<ToolbarAndroidChangeEvent>,
    /**
     * Callback called when the icon is selected.
     */
    onIconClicked?: DirectEventHandler<Readonly<{}>>,
    /**
     * Sets the overflow icon.
     */
    overflowIcon?: ImageSource,
    /**
     * Sets the toolbar subtitle.
     */
    subtitle?: string,
    /**
     * Sets the toolbar subtitle color.
     */
    subtitleColor?: ColorValue,
    /**
     * Sets the toolbar title.
     */
    title?: string,
    /**
     * Sets the toolbar title color.
     */
    titleColor?: ColorValue,
    /**
     * Sets the content inset for the toolbar starting edge.
     *
     * The content inset affects the valid area for Toolbar content other than
     * the navigation button and menu. Insets define the minimum margin for
     * these components and can be used to effectively align Toolbar content
     * along well-known gridlines.
     */
    contentInsetStart?: Int32,
    /**
     * Sets the content inset for the toolbar ending edge.
     *
     * The content inset affects the valid area for Toolbar content other than
     * the navigation button and menu. Insets define the minimum margin for
     * these components and can be used to effectively align Toolbar content
     * along well-known gridlines.
     */
    contentInsetEnd?: Int32,
    /**
     * Used to set the toolbar direction to RTL.
     * In addition to this property you need to add
     *
     *   android:supportsRtl="true"
     *
     * to your application AndroidManifest.xml and then call
     * `setLayoutDirection(LayoutDirection.RTL)` in your MainActivity
     * `onCreate` method.
     */
    rtl?: boolean,
    /**
     * Used to locate this view in end-to-end tests.
     */
    testID?: string,
};

export default codegenNativeComponent<ToolbarAndroidProps>(
    'ToolbarAndroid',
) as HostComponent<ToolbarAndroidProps>;