/**
 * SuiteCRM is a customer relationship management program developed by SuiteCRM Ltd.
 * Copyright (C) 2026 SuiteCRM Ltd.
 *
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Affero General Public License version 3 as published by the
 * Free Software Foundation with the addition of the following permission added
 * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
 * IN WHICH THE COPYRIGHT IS OWNED BY SUITECRM, SUITECRM DISCLAIMS THE
 * WARRANTY OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
 * details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * In accordance with Section 7(b) of the GNU Affero General Public License
 * version 3, these Appropriate Legal Notices must retain the display of the
 * "Supercharged by SuiteCRM" logo. If the display of the logos is not reasonably
 * feasible for technical reasons, the Appropriate Legal Notices must display
 * the words "Supercharged by SuiteCRM".
 */

import {Injectable} from "@angular/core";
import {BaseActionsAdapter} from "../../actions/base-action.adapter";
import {Observable, of} from "rxjs";
import {Action, ActionContext} from "../../../common/actions/action.model";
import {ViewMode} from "../../../common/views/view.model";
import {Process} from "../../process/process.service";
import {AsyncActionInput, AsyncActionService} from "../../process/processes/async-action/async-action";
import {MessageService} from "../../message/message.service";
import {ConfirmationModalService} from "../../modals/confirmation-modal.service";
import {LanguageStore} from "../../../store/language/language.store";
import {SelectModalService} from "../../modals/select-modal.service";
import {FieldModalService} from "../../modals/field-modal.service";
import {MetadataStore} from "../../../store/metadata/metadata.store.service";
import {AppMetadataStore} from "../../../store/app-metadata/app-metadata.store.service";
import {FieldLogicManager} from "../../../fields/field-logic/field-logic.manager";
import {AppStateStore} from "../../../store/app-state/app-state.store";
import {GlobalActionData} from "../actions/global.action";
import {GlobalActionManager} from "../actions/global-action-manager.service";

@Injectable({
    providedIn: 'root',
})
export class GlobalActionsAdapter extends BaseActionsAdapter<GlobalActionData> {

    constructor(
        protected actionManager: GlobalActionManager,
        protected asyncActionService: AsyncActionService,
        protected message: MessageService,
        protected confirmation: ConfirmationModalService,
        protected language: LanguageStore,
        protected selectModalService: SelectModalService,
        protected fieldModalService: FieldModalService,
        protected metadata: MetadataStore,
        protected appMetadataStore: AppMetadataStore,
        protected logic: FieldLogicManager,
        protected appState: AppStateStore,
    ) {
        super(
            actionManager,
            asyncActionService,
            message,
            confirmation,
            language,
            selectModalService,
            fieldModalService,
            metadata,
            appMetadataStore,
            logic
        );
    }

    initialized = false;

    init() {
        this.initialized = true;

        this.appState.asyncActionEventEmitter.subscribe((options) => {
            const action = options?.action as Action;
            const context = {
                id: action?.params.id,
                module: action?.params.module
            };
            this.runAction(action, context);
        });
    }

    protected buildActionData(action: Action, context: ActionContext): GlobalActionData {
        return {
            action: action,
        };
    }

    protected buildActionInput(action: Action, actionName: string, moduleName: string, context: ActionContext, actionData: GlobalActionData): AsyncActionInput {

        return {
            action: actionName,
            module: context.module,
            id: context.id,
            params: (action && action.params) || [],
        } as AsyncActionInput;
    }

    getActions(context: ActionContext): Observable<Action[]> {
        return of([]);
    }

    protected getMode(): ViewMode {
        return 'detail';
    }

    protected getModuleName(context: ActionContext): string {
        return "";
    }

    protected reload(action: Action, process: Process, context: ActionContext): void {
    }

}
