/**
 * Copyright (c) 2023, WSO2 LLC. (https://www.wso2.com). All Rights Reserved.
 *
 * This software is the property of WSO2 LLC. and its suppliers, if any.
 * Dissemination of any information or reproduction of any material contained
 * herein in any form is strictly forbidden, unless permitted by WSO2 expressly.
 * You may not alter or remove any copyright or other notice from copies of this content.
 */

import { HttpMethods } from "@wso2is/core/models";
import useRequest, {
    RequestErrorInterface,
    RequestResultInterface
} from "apps/console/src/features/core/hooks/use-request";
import { AxiosRequestConfig } from "axios";
import { store } from "../../../../../features/core/store";
import { AuthorizedAPIListItemInterface } from "../../models";

/**
 * Get Subscribed API resources.
 *
 * @param appId - application ID.
 * @returns `Promise<AuthorizedAPIListItemInterface[]>`
 * @throws `IdentityAppsApiException`
 */
export const useSubscribedAPIResources = <Data = AuthorizedAPIListItemInterface[], Error = RequestErrorInterface>(
    appId: string
): RequestResultInterface<Data, Error> => {

    const requestConfig: AxiosRequestConfig = {
        headers: {
            "Accept": "application/json",
            "Content-Type": "application/json"
        },
        method: HttpMethods.GET,
        url: `${ store.getState().config.endpoints.authzEndpoint }/applications/${ appId }/authorized-apis`
    };

    const { data, error, isValidating, mutate } = useRequest<Data, Error>(requestConfig);

    return {
        data,
        error: error,
        isLoading: !error && !data,
        isValidating,
        mutate
    };
};
