
// const isTouchDevice = ()=>{
//     return ('ontouchstart' in document.documentElement);
//     //return ("ontouchstart" in window || window.DocumentTouch && document instanceof DocumentTouch);
// }
  
const eventsHandling = (events)=>{
    let { onClick,onMouseDown,onMouseUp,onMouseMove,onMouseOut } = events;
    let convertEvents = {};
    if('ontouchstart' in document.documentElement && 
    (onClick || onMouseDown || onMouseUp || onMouseMove || onMouseOut)){
        if(onClick){
            convertEvents.onPointerUp = onClick;  
        }
        if(onMouseDown){
            convertEvents.onPointerDown = onMouseDown;
        }
        if(onMouseUp){
            convertEvents.onPointerUp = onMouseUp;
        }
        if(onMouseMove){
            convertEvents.onTouchMove = onMouseMove;
        }
        if(onMouseOut){
            convertEvents.onTouchOut = onMouseOut;
        }
        return Object.assign({},events,convertEvents);
    }
    else{
        return events;
    }
}

const useButton = (prop)=>{
    let { tagAttributeProps,
        disabled,
        text,
        elementType,
        ariaHasPopup,
        ariaExpanded,
        ariaControls,
        ariaPressed,
        ariaSelected,
        ariaLabel,
        ariaLabelledby,
        autoFocus,
        focusClass,
        onClick } = prop || {};
    let eventList = eventsHandling(Object.assign(tagAttributeProps,{onClick}))
    let additionalProps = {
        role: 'button',
        'aria-disabled': !disabled ? undefined : disabled,
        'aria-haspopup': ariaSelected,
        'aria-expanded': ariaExpanded,
        'aria-controls': ariaControls,
        'aria-pressed': ariaPressed,
        'aria-selected': ariaSelected,
        'aria-label': ariaLabel || text,
        'aria-labelledby': ariaLabelledby,
        'data-aria-autofocus': autoFocus || undefined,
        'data-aria-focusstyle': focusClass
    }
    return { buttonProps:Object.assign(additionalProps,eventList) };
}
const useSwitch = (prop)=>{
    let { tagAttributeProps,
        disabled,
        label,
        checked,
        onToggle
         } = prop || {};
         
    let eventList = eventsHandling(Object.assign(tagAttributeProps,{onClick:onToggle}));
    let additionalProps = {
        role: 'switch',
        'aria-disabled': !disabled ? undefined : disabled,
        'aria-label': label,
        'aria-labelledby': label,
        'aria-checked': checked
        
    }
    return { switchProps:Object.assign(additionalProps,eventList) };
}

let eventsObj = {
    'onMouseMove' : 'onTouchMove', /* not working touch device*/
    'onMouseOut' : 'onTouchOut', /* not working touch device*/
    'onMouseDown' : 'onPointerDown', /* not working touch device*/
    'onMouseUp' : 'onPointerUp' /* not working touch device*/
}

// components : {
//     Avatar : {
//         onClick : onPointer
//     },
    //    Button : {

    //    },
    //    ...
// }

export{
    useButton,
    useSwitch
};
