{"size": 2150, "ext": "js", "max_stars_repo_path": "assets/js/tasks.js", "max_stars_repo_name": "malcodeman/To-Do-List-App", "max_stars_repo_stars_event_min_datetime": "2020-03-20T15:23:09.000Z", "max_stars_repo_stars_event_max_datetime": "2020-03-20T15:23:09.000Z", "max_issues_repo_path": "assets/js/tasks.js", "max_issues_repo_name": "malcodeman/things-clone", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "assets/js/tasks.js", "max_forks_repo_name": "malcodeman/things-clone", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 1, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 21.9387755102, "max_line_length": 68, "alphanum_fraction": 0.6437209302}
import localStorage from "./localStorage.js";
import counter from "./counter.js";

function isSelectedAll() {
  const tasks = Array.from(document.getElementsByClassName("task"));

  return tasks.every(task => {
    return task.classList.contains("selected");
  });
}

function select() {
  const tasks = Array.from(document.getElementsByClassName("task"));

  tasks.forEach(task => {
    if (this === task) {
      this.classList.add("selected");
    } else {
      task.classList.remove("selected");
    }
  });
}

function selectFirst() {
  const tasks = Array.from(document.getElementsByClassName("task"));

  if (isSelectedAll()) {
    deselectAll();
  }

  const index = tasks.findIndex(task => {
    return task.classList.contains("selected");
  });

  if (index === -1) {
    tasks[0].classList.add("selected");
  } else if (index !== -1 && index !== tasks.length - 1) {
    tasks[index].classList.remove("selected");
    tasks[index + 1].classList.add("selected");
  }
}

function selectLast() {
  const tasks = Array.from(document.getElementsByClassName("task"));

  if (isSelectedAll()) {
    deselectAll();
  }

  const index = tasks.findIndex(task => {
    return task.classList.contains("selected");
  });

  if (index === -1) {
    tasks[tasks.length - 1].classList.add("selected");
  } else if (index !== -1 && index !== 0) {
    tasks[index].classList.remove("selected");
    tasks[index - 1].classList.add("selected");
  }
}

function selectAll() {
  const tasks = Array.from(document.getElementsByClassName("task"));

  tasks.forEach(task => {
    task.classList.add("selected");
  });
}

function deselectAll() {
  const tasks = Array.from(document.getElementsByClassName("task"));

  tasks.forEach(task => {
    task.classList.remove("selected");
  });
}

function remove() {
  const tasks = Array.from(document.getElementsByClassName("task"));

  tasks.forEach(task => {
    if (task.classList.contains("selected")) {
      task.remove();
      localStorage.removeTask(task.dataset.id);
      counter.renderCounter();
    }
  });
}

export default {
  select,
  selectFirst,
  selectLast,
  selectAll,
  deselectAll,
  remove
};
