import { Optional } from '@ephox/katamari';
import { Attribute, Value } from '@ephox/sugar';
import { Representing } from '../../api/behaviour/Representing';
// When showing a value in an input field, which part of the item do we use?
const setValueFromItem = (model, input, item) => {
    const itemData = Representing.getValue(item);
    Representing.setValue(input, itemData);
    setCursorAtEnd(input);
};
const setSelectionOn = (input, f) => {
    const el = input.element;
    const value = Value.get(el);
    const node = el.dom;
    // Only do for valid input types.
    if (Attribute.get(el, 'type') !== 'number') {
        f(node, value);
    }
};
const setCursorAtEnd = (input) => {
    setSelectionOn(input, (node, value) => node.setSelectionRange(value.length, value.length));
};
const setSelectionToEnd = (input, startOffset) => {
    setSelectionOn(input, (node, value) => node.setSelectionRange(startOffset, value.length));
};
const attemptSelectOver = (model, input, item) => {
    if (!model.selectsOver) {
        return Optional.none();
    }
    else {
        const currentValue = Representing.getValue(input);
        const inputDisplay = model.getDisplayText(currentValue);
        const itemValue = Representing.getValue(item);
        const itemDisplay = model.getDisplayText(itemValue);
        return itemDisplay.indexOf(inputDisplay) === 0 ?
            Optional.some(() => {
                setValueFromItem(model, input, item);
                setSelectionToEnd(input, inputDisplay.length);
            })
            : Optional.none();
    }
};
export { attemptSelectOver, setValueFromItem, setCursorAtEnd };
//# sourceMappingURL=TypeaheadModel.js.map