import { inject } from '@angular/core';
import { PERMANENT_STORAGE } from '@anglr/common';
import { Subject, Subscription } from 'rxjs';
import { PACKAGE_SOURCES } from '../../misc/tokens';
/**
 * Service used for obtaining available and used packages
 */
export class PackageManager {
    //######################### public properties #########################
    /**
     * Occurs when available packages changes
     */
    get packagesChange() {
        return this.packagesChangeSubject.asObservable();
    }
    /**
     * Gets available packages
     */
    get packages() {
        const result = [];
        for (const source of this.packageSources) {
            result.push(...source.packages);
        }
        return result;
    }
    /**
     * Gets current usedPackages value
     */
    get usedPackages() {
        return this._usedPackages;
    }
    set usedPackages(value) {
        this._usedPackages = value;
    }
    /**
     * Occurs when usedPackages changes
     */
    get usedPackagesChange() {
        return this.usedPackagesSubject.asObservable();
    }
    //######################### constructor #########################
    constructor(storageName) {
        this.storageName = storageName;
        //######################### private fields #########################
        /**
         * Current usedPackages value
         */
        this._usedPackages = [];
        //######################### protected properties #########################
        /**
         * Subscriptions created during initialization
         */
        this.initSubscriptions = new Subscription();
        /**
         * Used for emitting usedPackages changes
         */
        this.usedPackagesSubject = new Subject();
        /**
         * Used for emitting packages changes
         */
        this.packagesChangeSubject = new Subject();
        /**
         * Permanent storage storing selected packages
         */
        this.store = inject(PERMANENT_STORAGE);
        /**
         * Array of package source
         */
        this.packageSources = inject(PACKAGE_SOURCES);
        this.usedPackages = this.store.get(storageName) ?? [];
        for (const source of this.packageSources) {
            this.initSubscriptions.add(source.packagesChange.subscribe(() => this.packagesChangeSubject.next()));
        }
    }
    //######################### public methods #########################
    /**
     * Sets usedPackages new value
     * @param usedPackages - Value of usedPackages that changed
     */
    setUsedPackages(usedPackages) {
        if (this._usedPackages == usedPackages) {
            return;
        }
        this._usedPackages = usedPackages;
        this.store.set(this.storageName, usedPackages);
        this.usedPackagesSubject.next();
    }
    //######################### public methods #########################
    /**
     * Refresh available packages with current data
     */
    refresh() {
        for (const source of this.packageSources) {
            source.refresh();
        }
    }
    /**
     * Destroys service and all its resources
     */
    destroy() {
        this.initSubscriptions.unsubscribe();
    }
}
//# sourceMappingURL=packageManager.service.js.map