jQuery(function ($) {
	google.charts.load("current", { packages: ["corechart", "line", "table"] });
	// visits chart
	var wf_visits = document.getElementById("wf-visits");	
	
	if (typeof wf_visits != "undefined" && wf_visits != null) {
		google.charts.setOnLoadCallback(drawVisitsChart);
		
		// function drawVisitsChart() {
		// 	var data = google.visualization.arrayToDataTable(
		// 		WCAFFILIATE.charts.visits
		// 	);
		// 	var view = new google.visualization.DataView(data);
		// 	var options = {
		// 		legend: { position: "none" },
		// 		isStacked: true,
		// 		colors: ["#76a501"],
		// 	};
		// 	var chart = new google.visualization.ColumnChart(wf_visits);
		// 	chart.draw(view, options);
		// }
		

		function drawVisitsChart() {
			if (Array.isArray(WCAFFILIATE.charts.visits)) {
				var rawData = WCAFFILIATE.charts.visits;
			} else {
				var rawData = [
					[
						"Date",
						"Visits"
					],
					[
						"November",
						0
					],
					[
						"December",
						0
					],
					[
						"January",
						0
					],
					[
						"February",
						0
					],
					[
						"March",
						0
					],
					[
						"April",
						0
					]
				];
				
			}
			
			var transformedData = rawData.map(function (item, index) {
				if (index === 0) {
					return item;
				} else {
					var monthAbbrev = item[0].substring(0, 3);
					return [monthAbbrev, item[1]];
				}
			});
			var data = google.visualization.arrayToDataTable(transformedData);

			var options = {
				legend: { position: "none" },
				isStacked: true,
				colors: ["#76a501"],
			};

			var chart = new google.visualization.ColumnChart(wf_visits);
			chart.draw(data, options);
		}
	}

	// referrals chart
	var wf_referrals = document.getElementById("wf-referrals");
	if (typeof wf_referrals != "undefined" && wf_referrals != null) {
		google.charts.setOnLoadCallback(drawReferralsChart);
		function drawReferralsChart() {
			if (Array.isArray(WCAFFILIATE.charts.referrals)) {
				var rawData = WCAFFILIATE.charts.referrals;
			} else {
				var rawData = [
					[
						"Date",
						"Referrals"
					],
					[
						"November",
						0
					],
					[
						"December",
						0
					],
					[
						"January",
						0
					],
					[
						"February",
						0
					],
					[
						"March",
						0
					],
					[
						"April",
						0
					]
				];
			}
			var transformedData = rawData.map(function (item, index) {
				if (index === 0) {
					return item;
				} else {
					var monthAbbrev = item[0].substring(0, 3);
					return [monthAbbrev, item[1]];
				}
			});
			var data = google.visualization.arrayToDataTable(transformedData);
			var options = {
				legend: { position: "none" },
				isStacked: true,
				colors: ["#ff8e01"],
			};
			var chart = new google.visualization.ColumnChart(wf_referrals);
			chart.draw(data, options);
		}
	}

	// earnings chart
	var wf_earnings = document.getElementById("wf-earnings");
	if (typeof wf_earnings != "undefined" && wf_earnings != null) {
		google.charts.setOnLoadCallback(drawEarningsChart);
		function drawEarningsChart() {
			if (Array.isArray(WCAFFILIATE.charts.earnings)) {
				var rawData = WCAFFILIATE.charts.earnings;
			} else {
				var rawData = [
					[
						"Date",
						"Amount"
					],
					[
						"November",
						0
					],
					[
						"December",
						0
					],
					[
						"January",
						0
					],
					[
						"February",
						0
					],
					[
						"March",
						0
					],
					[
						"April",
						0
					]
				];
			}
			
			var transformedData = rawData.map(function (item, index) {
				if (index === 0) {
					return item;
				} else {
					var monthAbbrev = item[0].substring(0, 3);
					return [monthAbbrev, item[1]];
				}
			});
			var data = google.visualization.arrayToDataTable(transformedData);
			var options = {
				legend: { position: "none" },
				isStacked: true,
				colors: ["#5447c8"],
			};
			var chart = new google.visualization.ColumnChart(wf_earnings);
			chart.draw(data, options);
		}
	}

	// visits-referrals-earnings
	var wf_vre = document.getElementById("wf-visits-referrals-earnings");
	if (typeof wf_vre != "undefined" && wf_vre != null) {
		google.charts.setOnLoadCallback(drawVisitsReferralsEarningsChart);
		function drawVisitsReferralsEarningsChart() {
			if (Array.isArray(WCAFFILIATE.charts.visits_referrals_earnings)) {
				var rawData = WCAFFILIATE.charts.visits_referrals_earnings;
			} else {
				var rawData = [
					[
						"November",
						0,
						0,
						0
					],
					[
						"December",
						0,
						0,
						0
					],
					[
						"January",
						0,
						0,
						0
					],
					[
						"February",
						0,
						0,
						0
					],
					[
						"March",
						0,
						0,
						0
					],
					[
						"April",
						0,
						0,
						0
					]
				];
			}
			var transformedData = rawData.map(function (item, index) {
				if (index === 0) {
					return item;
				} else {
					var monthAbbrev = item[0].substring(0, 3);
					return [monthAbbrev].concat(item.slice(1));
				}
			});

			var data = new google.visualization.DataTable();
			data.addColumn("string", "");
			data.addColumn("number", "Visits");
			data.addColumn("number", "Referrals");
			data.addColumn("number", "Earnings");
			data.addRows(transformedData);
			var options = {
				chart: {},
				width: "100%",
				colors: ["#76a501", "#ff8e01", "#5447c8"],
			};
			var chart = new google.charts.Line(wf_vre);
			chart.draw(data, google.charts.Line.convertOptions(options));
		}
	}

	// conversion chart
	var wf_conversions = document.getElementById("wf-conversions");
	if (typeof wf_conversions != "undefined" && wf_conversions != null) {
		google.charts.setOnLoadCallback(drawConversionsChart);
		function drawConversionsChart() {
			if (Array.isArray(WCAFFILIATE.charts.conversions)) {
				var data = google.visualization.arrayToDataTable(
					Object.entries(WCAFFILIATE.charts.conversions)
				);
			} else {
				$('#wf-conversions').empty().append(
					$('<p>', {
						text: 'No data found.',
						css: {
							textAlign: 'center',
							color: '#000',
							margin: '20px 0',
						}
					})
				);

				const observer = new MutationObserver(function () {
					$('[id^="google-visualization-errors"]').hide();
				  });
				  
				  observer.observe(document.body, {
					childList: true,
					subtree: true,
				  });
			}
			
			var options = {
				is3D: false,
				colors: ["#0099c6", "#dd4477"],
			}; /* colors: [ converted, non-converted ] */
			var chart = new google.visualization.PieChart(wf_conversions);
			chart.draw(data, options);
		}
	}

	// products chart
	var wf_products = document.getElementById("wf-products");
	if (typeof wf_products != "undefined" && wf_products != null) {
		google.charts.setOnLoadCallback(drawProductsChart);
		function drawProductsChart() {
			if (Array.isArray(WCAFFILIATE.charts.products)) {
				var data = google.visualization.arrayToDataTable(
					Object.entries(WCAFFILIATE.charts.products)
				);
			} else {
				var data = [
					[
						"Product",
						"Sold"
					]
				];
			}
			
			var options = { is3D: true };
			var chart = new google.visualization.PieChart(wf_products);
			chart.draw(data, options);
		}
	}

	// affiliates table
	var wf_affiliates = document.getElementById("wf-affiliates");
	if (typeof wf_affiliates != "undefined" && wf_affiliates != null) {
		google.charts.setOnLoadCallback(drawAffiliatesTable);
		function drawAffiliatesTable() {
			var data = new google.visualization.DataTable();
			data.addColumn("string", "Affiliate Name");
			data.addColumn("number", "Visits");
			data.addColumn("number", "Referrals");
			data.addColumn("number", "Earnings");
			if (Array.isArray(WCAFFILIATE.charts.affiliates)) {
				data.addRows(WCAFFILIATE.charts.affiliates);
			}
			var options = {
				showRowNumber: true,
				width: "100%",
				height: "200",
				sortColumn: 3,
				sortAscending: false,
				cssClassNames: { headerRow: "cx-chart-header" },
			};
			var table = new google.visualization.Table(wf_affiliates);
			table.draw(data, options);
		}
	}

	// landingpages table
	var wf_landingpages = document.getElementById("wf-landingpages");
	if (typeof wf_landingpages != "undefined" && wf_landingpages != null) {
		google.charts.setOnLoadCallback(drawLandingpagesTable);
		function drawLandingpagesTable() {
			var data = new google.visualization.DataTable();
			data.addColumn("string", "Page URL");
			data.addColumn("number", "Visits");
			data.addColumn("number", "Referrals");
			if (Array.isArray(WCAFFILIATE.charts.landingpages)) {
				data.addRows(WCAFFILIATE.charts.landingpages);
			}
			var options = {
				showRowNumber: true,
				width: "100%",
				height: "200",
				sortColumn: 1,
				sortAscending: false,
				cssClassNames: { headerRow: "cx-chart-header" },
			};
			var table = new google.visualization.Table(wf_landingpages);
			table.draw(data, options);
		}
	}

	// referralurls table
	var wf_referralurls = document.getElementById("wf-referralurls");
	if (typeof wf_referralurls != "undefined" && wf_referralurls != null) {
		google.charts.setOnLoadCallback(drawReferralurlsTable);
		function drawReferralurlsTable() {
			var data = new google.visualization.DataTable();
			data.addColumn("string", "Page URL");
			data.addColumn("number", "Visits");
			data.addColumn("number", "Referrals");
			if (Array.isArray(WCAFFILIATE.charts.referralurls)) {
				data.addRows(WCAFFILIATE.charts.referralurls);
			}
			var options = {
				showRowNumber: true,
				width: "100%",
				height: "200",
				sortColumn: 1,
				sortAscending: false,
				cssClassNames: { headerRow: "cx-chart-header" },
			};
			var table = new google.visualization.Table(wf_referralurls);
			table.draw(data, options);
		}
	}

});
