// (C) 2025 GoodData Corporation
import { useMemo } from "react";
import { invariant } from "ts-invariant";
import { useBackendStrict, useOrganization, useWorkspace } from "@gooddata/sdk-ui";
export const useAutomationService = (scope) => {
    const backend = useBackendStrict();
    const workspace = useWorkspace();
    const organization = useOrganization();
    const service = useMemo(() => {
        if (scope === "organization") {
            invariant(organization, "organization is required when scope is 'organization'");
            return backend.organization(organization);
        }
        else {
            invariant(workspace, "workspace is required when scope is 'workspace'");
            return backend.workspace(workspace);
        }
    }, [backend, scope, workspace, organization]);
    return useMemo(() => ({
        //fetch data
        promiseGetAutomationsQuery: (params) => {
            if (scope === "organization") {
                return service
                    .automations()
                    .getAutomationsQuery({
                    includeAutomationResult: params?.includeAutomationResult,
                })
                    .withSize(params?.pageSize)
                    .withPage(params?.page)
                    .withFilter({
                    title: params?.search,
                })
                    .withWorkspace(params?.workspacesFilterQuery?.value, params?.workspacesFilterQuery?.type)
                    .withRecipient(params?.recipientsFilterQuery?.value, params?.recipientsFilterQuery?.type)
                    .withExternalRecipient(params?.externalRecipientsFilterQuery?.value)
                    .withAuthor(params?.createdByFilterQuery?.value, params?.createdByFilterQuery?.type)
                    .withStatus(params?.statusFilterQuery?.value, params?.statusFilterQuery?.type)
                    .withSorting([`${params?.sortBy},${params?.sortDirection}`])
                    .withType(params?.type)
                    .query();
            }
            else {
                return service
                    .automations()
                    .getAutomationsQuery({
                    includeAutomationResult: params?.includeAutomationResult,
                })
                    .withSize(params?.pageSize)
                    .withPage(params?.page)
                    .withFilter({
                    title: params?.search,
                })
                    .withDashboard(params?.dashboardFilterQuery?.value, params?.dashboardFilterQuery?.type)
                    .withRecipient(params?.recipientsFilterQuery?.value, params?.recipientsFilterQuery?.type)
                    .withExternalRecipient(params?.externalRecipientsFilterQuery?.value)
                    .withAuthor(params?.createdByFilterQuery?.value, params?.createdByFilterQuery?.type)
                    .withStatus(params?.statusFilterQuery?.value, params?.statusFilterQuery?.type)
                    .withSorting([`${params?.sortBy},${params?.sortDirection}`])
                    .withType(params?.type)
                    .query();
            }
        },
        //fetch permissions
        promiseGetCurrentUser: () => {
            return backend.currentUser().getUserWithDetails();
        },
        promiseCanManageWorkspace: () => {
            if (scope === "organization") {
                return Promise.resolve(undefined);
            }
            return service.permissions().getPermissionsForCurrentUser();
        },
        //fetch filter options
        promiseGetUsers: () => {
            if (scope === "organization") {
                return service.users().getUsersSummary();
            }
            else {
                return service.users().queryAll();
            }
        },
        promiseGetDashboards: () => {
            if (scope === "organization") {
                return Promise.resolve([]);
            }
            else {
                return service.dashboards().getDashboards();
            }
        },
        promiseGetWorkspaces: () => {
            if (scope === "organization") {
                return backend.workspaces().forCurrentUser().queryAllDescriptors();
            }
            else {
                return Promise.resolve([]);
            }
        },
        //actions
        promiseDeleteAutomation: (automation) => {
            if (scope === "organization") {
                return service
                    .automations()
                    .deleteAutomation(automation.id, automation.workspace?.id);
            }
            return service.automations().deleteAutomation(automation.id);
        },
        promiseDeleteAutomations: (automations) => {
            if (scope === "organization") {
                return service.automations().deleteAutomations(automations.map((automation) => ({
                    id: automation.id,
                    workspaceId: automation.workspace?.id,
                })));
            }
            return service
                .automations()
                .deleteAutomations(automations.map((automation) => automation.id));
        },
        promiseUnsubscribeAutomation: (automation) => {
            if (scope === "organization") {
                return service
                    .automations()
                    .unsubscribeAutomation(automation.id, automation.workspace?.id);
            }
            return service.automations().unsubscribeAutomation(automation.id);
        },
        promiseUnsubscribeAutomations: (automations) => {
            if (scope === "organization") {
                return service.automations().unsubscribeAutomations(automations.map((automation) => ({
                    id: automation.id,
                    workspaceId: automation.workspace?.id,
                })));
            }
            return service
                .automations()
                .unsubscribeAutomations(automations.map((automation) => automation.id));
        },
        promisePauseAutomation: (automation) => {
            if (scope === "organization") {
                return service
                    .automations()
                    .pauseAutomation(automation.id, automation.workspace?.id);
            }
            return service.automations().pauseAutomation(automation.id);
        },
        promisePauseAutomations: (automations) => {
            if (scope === "organization") {
                return service.automations().pauseAutomations(automations.map((automation) => ({
                    id: automation.id,
                    workspaceId: automation.workspace?.id,
                })));
            }
            return service
                .automations()
                .pauseAutomations(automations.map((automation) => automation.id));
        },
        promiseResumeAutomation: (automation) => {
            if (scope === "organization") {
                return service
                    .automations()
                    .resumeAutomation(automation.id, automation.workspace?.id);
            }
            return service.automations().resumeAutomation(automation.id);
        },
        promiseResumeAutomations: (automations) => {
            if (scope === "organization") {
                return service.automations().resumeAutomations(automations.map((automation) => ({
                    id: automation.id,
                    workspaceId: automation.workspace?.id,
                })));
            }
            return service
                .automations()
                .resumeAutomations(automations.map((automation) => automation.id));
        },
    }), [service, scope, backend]);
};
//# sourceMappingURL=useAutomationService.js.map