import { AppObjectUC } from "@vived/core";
import { ThemeColors } from "../../ThemeColors";
import { AppHandlerEntity, UnableToParsePayload, UnsupportedRequestVersion, } from "../Entities";
export class SetThemeColorUC extends AppObjectUC {
    constructor() {
        super(...arguments);
        this.requestType = "SET_THEME_COLORS";
        this.payloadVersion = 1;
    }
}
SetThemeColorUC.type = "SetThemeColorsBaseUC";
export function makeSetThemeColorUC(appObject) {
    return new SetThemeColorUCImp(appObject);
}
class SetThemeColorUCImp extends SetThemeColorUC {
    castPayloadV1(payload) {
        const castPayload = payload;
        if (castPayload.colors === undefined) {
            throw new UnableToParsePayload(this.requestType, 1, JSON.stringify(payload));
        }
        return castPayload;
    }
    constructor(appObject) {
        super(appObject, SetThemeColorUC.type);
        this.action = (payload) => {
            const { colors } = payload;
            const themeColors = ThemeColors.get(this.appObject.appObjectRepo);
            if (!themeColors) {
                return;
            }
            Object.keys(colors).forEach((schemeType) => {
                const type = schemeType;
                const hex = colors[schemeType];
                themeColors.setColorHexForType(type, hex);
            });
        };
        this.handleRequest = (version, payload) => {
            if (version === 1) {
                const castPayload = this.castPayloadV1(payload);
                this.action(castPayload);
            }
            else {
                throw new UnsupportedRequestVersion(this.requestType, version);
            }
        };
        const handler = appObject.getComponent(AppHandlerEntity.type);
        if (!handler) {
            this.warn("SetThemeColorsBaseUC has been added to an app object that does not have an AppHandlerEntity. Add one first");
            return;
        }
        handler.registerRequestHandler(this);
    }
}
//# sourceMappingURL=SetThemeColorUC.js.map