import { AppObjectUC } from "@vived/core";
import { StateMachineEntity } from "../../StateMachine";
import { EnvironmentEntity, Environments } from "../Entities/Environment";
export class SerializeEnvironment extends AppObjectUC {
    get environmentEntity() {
        return this.getCachedSingleton(EnvironmentEntity.type);
    }
    getSystemState() {
        if (!this.environmentEntity)
            return;
        const state = {
            environment: this.environmentEntity.currentEnvironment,
            intensity: this.environmentEntity.intensity,
        };
        return state;
    }
    applySystemState(state, version) {
        if (version === 1) {
            this.applyStateV1(state);
        }
        else if (version === 2) {
            this.applyStateV2(state);
        }
        else {
            this.applyDefaultState();
        }
    }
    applyStateV1(state) {
        if (!this.environmentEntity)
            return;
        if (state.environment === undefined) {
            this.applyDefaultState();
        }
        this.environmentEntity.currentEnvironment = state.environment;
        this.environmentEntity.intensity = this.defaultState.intensity;
    }
    applyStateV2(state) {
        if (!this.environmentEntity)
            return;
        if (state.environment === undefined || state.intensity === undefined) {
            this.applyDefaultState();
        }
        this.environmentEntity.currentEnvironment = state.environment;
        this.environmentEntity.intensity = state.intensity;
    }
    applyDefaultState() {
        if (!this.environmentEntity)
            return;
        this.environmentEntity.currentEnvironment = this.defaultState.environment;
        this.environmentEntity.intensity = this.defaultState.intensity;
    }
    constructor(appObject) {
        super(appObject, SerializeEnvironment.type);
        this.name = "textural-environment";
        this.seriliziationVersion = 2;
        this.defaultState = {
            environment: Environments.STUDIO,
            intensity: 0.75,
        };
        const stateMachine = StateMachineEntity.get(appObject.appObjectRepo);
        if (stateMachine) {
            stateMachine.registerSerializedSystem(this);
        }
        else {
            this.warn("Unable to find StateMachineEntity");
        }
    }
}
SerializeEnvironment.type = "SerializeEnvironment";
//# sourceMappingURL=SerializeEnvironment.js.map