{"size": 4001, "ext": "js", "max_stars_repo_path": "src/js/modules/slider.js", "max_stars_repo_name": "uvarov-frontend/istra-les", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "src/js/modules/slider.js", "max_issues_repo_name": "uvarov-frontend/istra-les", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "src/js/modules/slider.js", "max_forks_repo_name": "uvarov-frontend/istra-les", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 25.1635220126, "max_line_length": 130, "alphanum_fraction": 0.6423394151}
export default class Slider {
	constructor() {
		this.slider = undefined;
		this.btn = {
			prev: undefined,
			next: undefined,
		};

		this.x = undefined;
		this.y = undefined;

		// Активный слайд по умолчанию
		this.IDActiveSlide = 0;
	}

	removeActiveItem() {
		this.slideActive.classList.remove('slide-show');
		if (this.point.element) this.point.element[this.IDActiveSlide].classList.remove('point-active');
	}

	setActiveSlide() {
		this.slideActive = this.slide.all[this.IDActiveSlide];
		this.slideActive.classList.add('slide-show');
	}

	setActivePoint() {
		this.point.element[this.IDActiveSlide].classList.add('point-active');
	}

	getPoint() {
		this.point.element = this.point.elements.querySelectorAll(`.${this.point.class}`);
	}

	createPoint() {
		if (!this.point.elements) return;
		const point = document.createElement('span');
		point.className = this.point.class;
		this.point.elements.append(point);
	}

	getSlide() {
		if (this.effect === 'fade') this.setActiveSlide();
		if (!this.point.elements) return;
		// eslint-disable-next-line no-plusplus
		// for (let i = 0; i < this.slide.all.length; i++) {
		// 	this.createPoint();
		// }
		this.getPoint();
		this.setActivePoint();
	}

	scrollNext() {
		if (this.slide.all.length > this.slide.count && this.scroll < this.scrollMax) {
			this.scroll += this.slide.all[1].offsetLeft;
		} else {
			this.scroll = 0;
		}
		this.list.style.transform = `translateX(-${this.scroll}px)`;
	}

	scrollPrev() {
		if (this.slide.all.length > this.slide.count && this.scroll > 0) {
			this.scroll -= this.slide.all[1].offsetLeft;
		} else {
			this.scroll = this.scrollMax;
		}
		this.list.style.transform = `translateX(-${this.scroll}px)`;
	}

	clickPrev() {
		if (this.effect === 'fade') {
			this.removeActiveItem();
			if (this.slideActive.previousElementSibling) {
				this.IDActiveSlide -= 1;
			} else {
				this.IDActiveSlide = this.slide.all.length - 1;
			}
			this.setActiveSlide();
			this.setActivePoint();
		} else if (this.effect === 'scroll' && this.list) {
			this.scrollPrev();
		}
	}

	clickNext() {
		if (this.effect === 'fade') {
			this.removeActiveItem();
			if (this.slideActive.nextElementSibling) {
				this.IDActiveSlide += 1;
			} else {
				this.IDActiveSlide = 0;
			}
			this.setActiveSlide();
			this.setActivePoint();
		} else if (this.effect === 'scroll' && this.list) {
			this.scrollNext();
		}
	}

	getPrev() {
		this.btn.prev.addEventListener('click', () => {
			this.clickPrev();
		});
	}

	getNext() {
		this.btn.next.addEventListener('click', () => {
			this.clickNext();
		});
	}

	addTouch() {
		this.slider.addEventListener('touchstart', (e) => {
			this.x = e.changedTouches[0].clientX;
			this.y = e.changedTouches[0].clientY;
		});

		this.slider.addEventListener('touchend', (e) => {
			if (this.y > (e.changedTouches[0].clientY + 60) || this.y < (e.changedTouches[0].clientY - 60)) {
				return;
			}

			if ((this.x + 30) < e.changedTouches[0].clientX) {
				this.clickPrev();
			} else if ((this.x - 30) > e.changedTouches[0].clientX) {
				this.clickNext();
			}
		});
	}

	init(options) {
		this.slider = document.querySelector(`${options.selector}`);
		if (!this.slider) return;

		this.list = this.slider.querySelector('[data-slider="list"]');
		this.slide = {
			count: options.count ? options.count : 1,
			all: this.slider.querySelectorAll('[data-slider="slide"]'),
		};
		this.point = {
			class: options.point?.class ? options.point.class : undefined,
			elements: this.slider.querySelector('[data-slider="points"]'),
			element: undefined,
		};
		this.btn = {
			prev: this.slider.querySelector('[data-slider="btn-prev"]'),
			next: this.slider.querySelector('[data-slider="btn-next"]'),
		};
		this.effect = options.effect ? options.effect : 'fade';
		this.scroll = 0;
		this.scrollMax = this.list && this.slide.all[1] ? this.list.scrollWidth - (this.slide.all[1].offsetLeft * this.slide.count) : 0;

		this.getSlide();
		this.getPrev();
		this.getNext();
		this.addTouch();
	}
}
