import { toggleMark } from 'prosemirror-commands';
import { isMarkActive } from "../../../utils/marks.js";
import { ColorSpecs, colorType } from "./ColorSpecs/index.js";
import { colorAction, colorMarkName } from "./const.js";
import { chainAND, parseStyleColorValue, validateClassNameColorName } from "./utils.js";
import "./colors.css";
export { colorClassName, Colors } from "./const.js";
export { colorMarkName, colorType } from "./ColorSpecs/index.js";
export const Color = (builder) => {
    builder.use(ColorSpecs, {
        validateClassNameColorName,
        parseStyleColorValue,
    });
    builder.addAction(colorAction, ({ schema }) => {
        const type = colorType(schema);
        return {
            isActive: (state) => Boolean(isMarkActive(state, type)),
            isEnable: toggleMark(type),
            run: (state, dispatch, _view, attrs) => {
                const params = attrs;
                const hasMark = isMarkActive(state, type);
                if (!params || !params[colorMarkName]) {
                    if (!hasMark)
                        return true;
                    // remove mark
                    return toggleMark(type, params)(state, dispatch);
                }
                if (hasMark) {
                    // remove old mark, then add new with new color
                    return chainAND(toggleMark(type), toggleMark(type, params))(state, dispatch);
                }
                // add mark
                return toggleMark(type, params)(state, dispatch);
            },
            meta(state) {
                return type.isInSet(state.selection.$to.marks())?.attrs[colorMarkName];
            },
        };
    });
};
//# sourceMappingURL=index.js.map
