import { createTrait } from '@meta-ui/core';
import { Type } from '@sinclair/typebox';
const HasInitializedMap = new Map();
const useStateTrait = ({ key, initialValue, componentId, mergeState, subscribeMethods, }) => {
    const hashId = `#${componentId}@${key}`;
    const hasInitialized = HasInitializedMap.get(hashId);
    if (!hasInitialized) {
        mergeState({ [key]: initialValue });
        const methods = {
            setValue({ key, value }) {
                mergeState({ [key]: value });
            },
            resetValue({ key }) {
                mergeState({ [key]: initialValue });
            },
        };
        subscribeMethods(methods);
        HasInitializedMap.set(hashId, true);
    }
    return {
        props: null,
    };
};
const PropsSchema = Type.Object({
    key: Type.String(),
    initialValue: Type.Any(),
});
export default Object.assign(Object.assign({}, createTrait({
    version: 'core/v1',
    metadata: {
        name: 'state',
        description: 'add state to component',
    },
    spec: {
        properties: PropsSchema,
        state: Type.Any(),
        methods: [
            {
                name: 'setValue',
                parameters: Type.Object({
                    key: Type.String(),
                    value: Type.Any(),
                }),
            },
            {
                name: 'reset',
            },
        ],
    },
})), { impl: useStateTrait });
//# sourceMappingURL=state.js.map