// Copyright (c) 2017, JINGROW
// For license information, please see license.txt

jingrow.webhook = {
	set_fieldname_select: (frm) => {
		if (frm.pg.webhook_pagetype) {
			jingrow.model.with_pagetype(frm.pg.webhook_pagetype, () => {
				// get pagetype fields
				let fields = $.map(
					jingrow.get_pg("PageType", frm.pg.webhook_pagetype).fields,
					(d) => {
						if (
							jingrow.model.no_value_type.includes(d.fieldtype) &&
							!jingrow.model.table_fields.includes(d.fieldtype)
						) {
							return null;
						} else {
							return {
								label: `${__(d.label, null, d.parent)} (${__(d.fieldtype)})`,
								value: d.fieldname,
							};
						}
					}
				);

				// add meta fields
				for (let field of jingrow.model.std_fields) {
					if (field.fieldname == "name") {
						fields.unshift({ label: __("Name (Pg Name)"), value: "name" });
					} else {
						fields.push({
							label: `${__(field.label, null, field.parent)} (${__(
								field.fieldtype
							)})`,
							value: field.fieldname,
						});
					}
				}

				frm.fields_dict.webhook_data.grid.update_pagefield_property(
					"fieldname",
					"options",
					[""].concat(fields)
				);
			});
		}
	},

	set_request_headers: (frm) => {
		if (frm.pg.request_structure) {
			let header_value;
			if (frm.pg.request_structure == "Form URL-Encoded") {
				header_value = "application/x-www-form-urlencoded";
			} else if (frm.pg.request_structure == "JSON") {
				header_value = "application/json";
			}

			if (header_value) {
				let header_row = (frm.pg.webhook_headers || []).find(
					(row) => row.key === "Content-Type"
				);
				if (header_row) {
					jingrow.model.set_value(
						header_row.pagetype,
						header_row.name,
						"value",
						header_value
					);
				} else {
					frm.add_child("webhook_headers", {
						key: "Content-Type",
						value: header_value,
					});
				}
				frm.refresh();
			}
		}
	},
};

jingrow.ui.form.on("Webhook", {
	refresh: (frm) => {
		jingrow.webhook.set_fieldname_select(frm);
		frm.set_query(
			"background_jobs_queue",
			"jingrow.integrations.pagetype.webhook.webhook.get_all_queues"
		);
	},

	request_structure: (frm) => {
		jingrow.webhook.set_request_headers(frm);
	},

	webhook_pagetype: (frm) => {
		jingrow.webhook.set_fieldname_select(frm);
	},

	enable_security: (frm) => {
		frm.toggle_reqd("webhook_secret", frm.pg.enable_security);
	},

	preview_document: (frm) => {
		jingrow.call({
			method: "generate_preview",
			pg: frm.pg,
			callback: (r) => {
				frm.refresh_field("meets_condition");
				frm.refresh_field("preview_request_body");
			},
		});
	},
});

jingrow.ui.form.on("Webhook Data", {
	fieldname: (frm, cdt, cdn) => {
		let row = locals[cdt][cdn];
		let df = jingrow
			.get_meta(frm.pg.webhook_pagetype)
			.fields.filter((field) => field.fieldname == row.fieldname);

		if (!df.length) {
			// check if field is a meta field
			df = jingrow.model.std_fields.filter((field) => field.fieldname == row.fieldname);
		}

		row.key = df.length ? df[0].fieldname : "name";
		frm.refresh_field("webhook_data");
	},
});
