package runtime

import (
	"context"
	"strings"
)

// SetWorkspaceScope installs the workspace-profile ownership resolver before
// the runner starts. All-profile runtimes leave it nil.
func (r *ServerRunner) SetWorkspaceScope(scope func(context.Context) ([]string, error)) {
	if r == nil {
		return
	}
	r.workspaceScope = scope
}

func (r *ServerRunner) currentWorkspaceScope(ctx context.Context) ([]string, error) {
	if r == nil || r.workspaceScope == nil {
		return nil, nil
	}
	workspaceIDs, err := r.workspaceScope(ctx)
	if err != nil {
		return nil, err
	}
	if workspaceIDs == nil {
		return []string{}, nil
	}
	return workspaceIDs, nil
}

func containsWorkspaceID(workspaceIDs []string, workspaceID string) bool {
	workspaceID = strings.TrimSpace(workspaceID)
	for _, candidate := range workspaceIDs {
		if strings.TrimSpace(candidate) == workspaceID {
			return true
		}
	}
	return false
}
