<?php

namespace Kanzu\StoreVoiceAI;

// Prevent direct file access.
if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

class Service_Container {

    private $factories = [];
    private $instances = [];

    public function set( $id, callable $factory ) {
        $this->factories[ $id ] = $factory;
        unset( $this->instances[ $id ] );
    }

    public function get( $id ) {
        if ( ! array_key_exists( $id, $this->instances ) ) {
            if ( ! isset( $this->factories[ $id ] ) ) {
                throw new \InvalidArgumentException( esc_html( sprintf( 'Unknown service ID requested: %s', $id ) ) );
            }
            $this->instances[ $id ] = ( $this->factories[ $id ] )( $this );
        }
        return $this->instances[ $id ];
    }

    public function has( $id ) {
        return isset( $this->factories[ $id ] );
    }
}
