import { defineStore } from "pinia";

const STORAGE_KEY = "selectedCompany";

export const useCompanyStore = defineStore("company", {
	state: () => ({
		companies: ["Sprinklers Northwest", "Nuco Yard Care", "Lowe Fencing", "Veritas Stone", "Daniels Landscape Supplies"],
		selectedCompany: localStorage.getItem(STORAGE_KEY) || "Sprinklers Northwest",
	}),

	getters: {
		currentCompany: (state) => state.selectedCompany,
	},

	actions: {
		setSelectedCompany(companyName) {
			if (this.companies.includes(companyName)) {
				this.selectedCompany = companyName;
				localStorage.setItem(STORAGE_KEY, companyName);
			}
		},

		setCompanies(companies = []) {
			this.companies = [...companies];
			if (!this.companies.includes(this.selectedCompany)) {
				this.selectedCompany = this.companies[0] || null;
				if (this.selectedCompany) {
					localStorage.setItem(STORAGE_KEY, this.selectedCompany);
				}
			}
		},
	},
});
