import {MouseEvent} from 'react'
import {isUndefined} from 'lodash-es'
import {
    createDelayActionQueue,
    isDev,
} from '@befe/brick-utils'
import {Link} from '@befe/brick-comp-link'
import {isActionList} from '../utils/picker-utils'
import {TypeActionLink} from './prop-types'


export class QuickActions {

    renderQuickActions = (
        {
            getValue,
            getActionQueue,
            syncDisplayDate,
            onMouseDown,
            renderQuickActions,
        }: {
            // eslint-disable-next-line @typescript-eslint/no-explicit-any
            getValue: () => any
            getActionQueue: () => ReturnType<typeof createDelayActionQueue>
            syncDisplayDate: () => void
            // 目前用于, 取消 blur 的默认行为, 保证 date picker input 的聚焦状态
            onMouseDown?: (e: MouseEvent) => void
            renderQuickActions?: () => TypeActionLink[]
        }
    ) => {

        if (renderQuickActions) {
            const quickActions = renderQuickActions()
            let quickActionsElem = null

            if (isActionList(quickActions)) {
                quickActionsElem = quickActions.map(
                    action => {
                        let handleActionClick = action.onClick

                        if (!isUndefined(getValue())) {
                            handleActionClick = () => {
                                if (action.onClick) {
                                    const clickResult = action.onClick()
                                    /**
                                     * @note: undefined | false => 自动同步 display date
                                     *
                                     * - 如果为 true, 则跳过同步 display date, 这样可以保持对 display date 的同步
                                     *  - 因为大多数场景, quick action 是一个 "设定新的取值" 的过程
                                     */
                                    const shouldSyncDisplayDate = clickResult !== true

                                    if (shouldSyncDisplayDate) {
                                        getActionQueue().pushAction(() => {
                                            syncDisplayDate()
                                        })
                                    }
                                }
                            }
                        }

                        return (
                            <Link
                                className={'brick-quick-action'}
                                key={action.key}
                                onClick={handleActionClick}
                            >
                                {action.text}
                            </Link>
                        )
                    }
                )
            } else if (isDev()) {
                console.warn('renderQuickActions 格式为: '
                    + '() => [{key, text, onClick: () => undefined | false | true }]')
            }

            if (quickActionsElem) {
                return (
                    <div
                        className={'brick-date-picker-quick-actions'}
                        onMouseDown={onMouseDown}
                    >
                        {quickActionsElem}
                    </div>
                )
            }

        }
        return undefined
    }


}
