/**
# Copyright (c) 2024, HCUOpt CORPORATION.  All rights reserved.
**/

package oci

import (
	"fmt"
	"strings"

	"github.com/opencontainers/runtime-spec/specs-go"
)

type memorySpec struct {
	*specs.Spec
}

// NewMemorySpec creates a Spec instance from the specified OCI spec
func NewMemorySpec(spec *specs.Spec) Spec {
	s := memorySpec{
		Spec: spec,
	}

	return &s
}

// Load is a no-op for the memorySpec spec
func (s *memorySpec) Load() (*specs.Spec, error) {
	return s.Spec, nil
}

// Flush is a no-op for the memorySpec spec
func (s *memorySpec) Flush() error {
	return nil
}

// Modify applies the specified SpecModifier to the stored OCI specification.
func (s *memorySpec) Modify(m SpecModifier) error {
	if s.Spec == nil {
		return fmt.Errorf("cannot modify nil spec")
	}
	return m.Modify(s.Spec)
}

// LookupEnv mirrors os.LookupEnv for the OCI specification. It
// retrieves the value of the environment variable named
// by the key. If the variable is present in the environment the
// value (which may be empty) is returned and the boolean is true.
// Otherwise the returned value will be empty and the boolean will
// be false.
func (s memorySpec) LookupEnv(key string) (string, bool) {
	if s.Spec == nil || s.Spec.Process == nil {
		return "", false
	}

	for _, env := range s.Spec.Process.Env {
		if !strings.HasPrefix(env, key) {
			continue
		}

		parts := strings.SplitN(env, "=", 2)
		if parts[0] == key {
			if len(parts) < 2 {
				return "", true
			}
			return parts[1], true
		}
	}

	return "", false
}
