
export const DEFAULT_SUPPORTED_HEADINGS = ['heading1', 'heading2', 'heading3'];//It is already defined by the heading plugin
export const TOC_CUSTOM_PROPERTY = 'toc';
export const TOC_COMMAND = 'inserttoc';
export const TOC_ATTRIBUTES = {
	HEADING_CLASS: 'headingClass',
	ANCHOR_ID: 'anchorId', //It is already defined by the anchor plugin,
	LINK_HREF: 'linkHref'//It is already defined by the link plugin
}
 
export const TOC_ELEMENTS = {
	TOC: 'toc',
	PARAGRAPH: 'paragraph' //It is already defined by the paragraph plugin
}

export function normalizeHeadingOptions(headingOptions) {
	let headings = headingOptions || [];
	headings = headings.map(value => value.toLowerCase()).filter(value => value.startsWith('heading'));
	if(headings.length == 0){
		headings = DEFAULT_SUPPORTED_HEADINGS;
	}
	return headings;
}

export function rangeToText(range) {
	return Array.from(range.getItems()).reduce((rangeText, node) => {
		if (!(node.is('text') || node.is('textProxy'))) {
			return rangeText;
		}
		return rangeText + node.data;
	}, '');
}

export function getAttributeValueOn(range, attributeName) {
	for (const item of range.getItems()) {
		for (const [ key, value ] of item.getAttributes()) {
			if (key.toLowerCase() == attributeName.toLowerCase()) {
				return value;
			}
		}
	}
    return null;
}

export function setAttributeOn(range, schema, writer, attribute) {
	for (const node of Array.from(range.getItems())) {
		if (schema.checkAttribute(node, attribute.key)) {
			writer.setAttribute(attribute.key, attribute.value, node);
			return true;
		}
	}
    return false;
}

export function getTextNodeOn(range) {
	for (const node of Array.from(range.getItems())) {
		if (node.is('text') || node.is('textProxy')) {
			return node;
		}
	}
    return null;
}