// Part of the Chili3d Project, under the AGPL-3.0 License.
// See LICENSE file in the project root for full license information.

import { command, I18n, type IApplication, type ICommand, PubSub } from "@chili3d/core";

@command({
    key: "doc.save",
    icon: "icon-save",
    isApplicationCommand: true,
})
export class SaveDocument implements ICommand {
    async execute(app: IApplication): Promise<void> {
        const document = app.activeView?.document;
        if (!document) return;
        PubSub.default.pub(
            "showPermanent",
            async () => {
                await document.save();
                PubSub.default.pub("documentSaved", document);
                PubSub.default.pub("showToast", "toast.document.saved");
            },
            "toast.executing{0}",
            I18n.translate("command.doc.save"),
        );
    }
}
