function saveProfileForCloseProfile(profile, actionTag, fcn, peInstance)
    
    switch actionTag
        case 'saveButtonAction'
            [profileIsInUse, modelsToSave] = pe.toolstrip.profileHasOpenUsages(profile, peInstance);
            if profileIsInUse
                dp = DAStudio.DialogProvider;
                qDlg = dp.questdlg(...
                    DAStudio.message('SystemArchitecture:ProfileDesigner:SaveProfileAndModelsQuestion', profile.getName()), ...
                    DAStudio.message('SystemArchitecture:ProfileDesigner:SaveProfileTitle'), ...
                    {DAStudio.message('SystemArchitecture:ProfileDesigner:SaveProfileAndModels'), ...
                    DAStudio.message('SystemArchitecture:ProfileDesigner:SaveProfileOnly'), ...
                    DAStudio.message('SystemArchitecture:ProfileDesigner:Cancel')}, ...
                    DAStudio.message('SystemArchitecture:ProfileDesigner:SaveProfileAndModels'), ...
                    @(resp) handleSaveResponse(resp, modelsToSave, profile, fcn));
            else
                handleSaveResponse(...
                    DAStudio.message('SystemArchitecture:ProfileDesigner:SaveProfileOnly'), modelsToSave, profile, fcn, peInstance);
            end
        case 'saveAsButtonAction'
            pe.toolstrip.profileSaveAs(profile, []);
            systemcomposer.loadProfile(profile);
        case 'saveAllButtonAction'
            pe.toolstrip.saveUnsavedProfiles();
        case 'exportToPreviousButtonAction'
            pe.toolstrip.handleClickExportProfile(profile);
            systemcomposer.loadProfile(profile);
        otherwise
            assert(false, 'Invalid action tag');
    end
    
    function handleSaveResponse(response, modelsToSave, profile, fcn, peInstance)
    
            prf = profile;
            switch response
                case DAStudio.message('SystemArchitecture:ProfileDesigner:SaveProfileAndModels')
                case DAStudio.message('SystemArchitecture:ProfileDesigner:SaveProfileOnly')
                    modelsToSave = {};
                otherwise
                    return; % cancel => do nothing
            end
    
            % Does profile have a filepath where it came from?
            if ~isempty(prf.filePath)
                s = pe.toolstrip.profileSave(prf, '', fcn);
                if s % either error or abort
                    return;
                end
            else
                pe.toolstrip.profileSaveAsForCloseProfile(prf, fcn, peInstance);
            end
    
            % Now save any open models or dictionaries
            for idx = 1 : length(modelsToSave)
                [~,name,ext] = fileparts(modelsToSave{idx});
                if ~strcmpi(ext, '.sldd')
                    save_system(name, 'SaveDirtyReferencedModels', true);
                else
                    ddConn = Simulink.data.dictionary.open(modelsToSave{idx});
                    ddConn.saveChanges();
                end
            end
        end
end