/**
 * MIT License
 *
 * Copyright (C) 2026 Huawei Device Co., Ltd.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

import React from 'react';
import * as ReactNative from "react-native";
import MJRefreshView, { Commands, NativeProps, VoidEventData, OnPullingEventData } from "./src/js/MJRefreshNativeComponent";
import MJScrollView from './MJScrollView'
import MJFlatlist from './MJFlatList'

class MJRefresh extends React.Component<NativeProps>  {
    mjRefreshRef: React.ElementRef<typeof MJRefresh> | null =
        null;
    _onMJRefresh = (e: ReactNative.NativeSyntheticEvent<VoidEventData>) => {
        let { onRefresh } = this.props;
        onRefresh && onRefresh(e);
    }
    _onMJPulling = (e: ReactNative.NativeSyntheticEvent<OnPullingEventData>) => {
        let { onPulling } = this.props;
        onPulling && onPulling(e);
    }
    _onMJReleaseToRefresh = (e: ReactNative.NativeSyntheticEvent<VoidEventData>) => {
        let { onReleaseToRefresh } = this.props;
        onReleaseToRefresh && onReleaseToRefresh(e);
    }
    _onMJRefreshIdle = (e: ReactNative.NativeSyntheticEvent<VoidEventData>) => {
        let { onRefreshIdle } = this.props;
        onRefreshIdle && onRefreshIdle(e);
    }

    finishRefresh = (
    ) => {
        if (this.mjRefreshRef) {
            Commands.finishRefresh(
                this.mjRefreshRef
            );
        }
    };

    beginRefresh = (
    ) => {
        if (this.mjRefreshRef) {
            Commands.beginRefresh(
                this.mjRefreshRef
            );
        }
    };
    render() {
        const nativeProps = {
            ...this.props,
            ...{
                onRefresh: this._onMJRefresh,
                onRefreshIdle: this._onMJRefreshIdle,
                onReleaseToRefresh: this._onMJReleaseToRefresh,
                onPulling: this._onMJPulling,
            },
        };
        return (
            <MJRefreshView
                ref={(ref) => {
                    this.mjRefreshRef = ref;

                }}

                {...nativeProps}
            >
                {this.props.children}
            </MJRefreshView>
        );
    }
}
export const ScrollView = MJScrollView;
export const FlatList = MJFlatlist;
export default MJRefresh;