import customSelect from 'custom-select';
 
(function(window, document, cutme, undefined) {    
    
    // Select
    
    (function() {
        
        const select = document.getElementsByTagName('select');
    
        const init = function() {
    
            const cstSel = customSelect(select);
            
            const radioAndSelect = function() {
            
                const el = document.getElementsByClassName('js-radioAndSelect');
               
                for (let j = 0; j < el.length; j++) {
    
                    const radio = el[j].getElementsByTagName('input');
        
                    for (let i = 0; i < radio.length; i++) {
                        radio[i].addEventListener('change', function(e) {
                            
                            let changed = el[j].getElementsByClassName('is-changed')[0];
                            changed ? changed.classList.remove('is-changed') : false;
                        });
                    }
                }
            };

            for (let i = 0; i < select.length; i ++) {

                cstSel[i].container.addEventListener('custom-select:open', (e) => { 
                    e.target.parentNode.style.position = 'relative';
                    e.target.parentNode.style.zIndex = '5';
                });
                
                cstSel[i].container.addEventListener('custom-select:close', (e) => { 
                    e.target.parentNode.removeAttribute('style');
                });
                
                cstSel[i].select.addEventListener('change', (e) => { 
                    e.target.parentNode.parentNode.classList.add('is-changed');
                    
                    let parent = e.target.parentNode.parentNode.parentNode.parentNode,
                        inputRadio = parent.querySelectorAll('input');
                        
                    
                    for (let i = 0; i < inputRadio.length; i ++) {
                        inputRadio[i].checked = false;
                    }
                });
            }
            
            radioAndSelect();
        };
                
        select ? init() : false;
        
    })();
    

}(window, document, window.cutme = window.cutme  || {}));