/** @format */

import * as React from 'react';
import Axios from '../utils/xhr';
import {GET_BRTCH_LIST} from '../const/path';
import {GET_PROVIDER_PLATFORM_LIST} from '../const/path';

interface IbaseQuery {
    page_number: number;
    is_all?: boolean;
    page_size?: number;
}
const page_size = 10;

export interface IsourceItem {
    api_name: string;
    api_router: string;
    created_at: string;
    download_file: string;
    file_name: string;
    id: number;
    status: string;
}

/**
 * 获取数据源接口
 */
export const usePlatformApi = () => {
    const [state, setState] = React.useState({
        loading: false,
        error: null,
        dataSource: [],
    });
    const requestData = () => {
        setState(preState => ({...preState, loading: false}));
        Axios({
            url: GET_PROVIDER_PLATFORM_LIST,
        }).then(({code, msg, data}) => {
            if (code === 0) {
                if (!data.provider_api_base_info_list) {
                    data.provider_api_base_info_list = [];
                }
                setState(preState => ({
                    ...preState,
                    loading: false,
                    dataSource: data.provider_api_base_info_list
                        .map((element: any) => {
                            return {
                                value: element.provider_api_id,
                                label: element.thirdpart_api_name + '-' + element.platform_name,
                                key: element.provider_api_id,
                                router: element.thirdpart_host,
                            };
                        })
                        .sort((pre: any, next: any) => {
                            return pre.label.localeCompare(next.label);
                        }),
                }));
            } else {
                setState(preState => ({...preState, loading: false, error: msg}));
            }
        });
    };
    React.useEffect(() => {
        requestData();
    }, []);
    return [state, requestData] as const;
};
