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

jingrow.ui.form.on("Energy Point Rule", {
	validate(frm) {
		frm.set_df_property("user_field", "reqd", !frm.pg.for_assigned_users);
		frm.set_df_property("condition", "reqd", frm.pg.for_pg_event === "Custom");
	},
	refresh(frm) {
		frm.events.set_field_options(frm);
	},
	for_pg_event(frm) {
		if (frm.pg.for_assigned_users) {
			frm.set_value("for_assigned_users", !frm.pg.for_pg_event === "New");
		}
	},
	reference_pagetype(frm) {
		frm.events.set_field_options(frm);
	},
	set_field_options(frm) {
		// sets options for field_to_check, user_field and multiplier fields
		// based on reference pagetype
		const reference_pagetype = frm.pg.reference_pagetype;
		if (!reference_pagetype) return;

		jingrow.model.with_pagetype(reference_pagetype, () => {
			const map_for_options = (df) => ({ label: df.label, value: df.fieldname });
			const fields = jingrow.meta
				.get_pagefields(frm.pg.reference_pagetype)
				.filter(jingrow.model.is_value_type);

			const fields_to_check = fields.map(map_for_options);

			const user_fields = fields
				.filter(
					(df) =>
						(df.fieldtype === "Link" && df.options === "User") ||
						df.fieldtype === "Data"
				)
				.map(map_for_options)
				.concat([
					{ label: __("Owner"), value: "owner" },
					{ label: __("Modified By"), value: "modified_by" },
				]);

			const multiplier_fields = fields
				.filter((df) => ["Int", "Float"].includes(df.fieldtype))
				.map(map_for_options);

			// blank option for the ability to unset the multiplier field
			multiplier_fields.unshift(null);

			frm.set_df_property("field_to_check", "options", fields_to_check);
			frm.set_df_property("user_field", "options", user_fields);
			frm.set_df_property("multiplier_field", "options", multiplier_fields);
		});
	},
});
