// Copyright (c) 2015, JINGROW and Contributors
// MIT License. See license.txt

jingrow.provide("jingrow.workflow");

jingrow.workflow = {
	state_fields: {},
	workflows: {},
	avoid_status_override: {},
	setup: function (pagetype) {
		var wf = jingrow.get_list("Workflow", { document_type: pagetype });
		if (wf.length) {
			jingrow.workflow.workflows[pagetype] = wf[0];
			jingrow.workflow.state_fields[pagetype] = wf[0].workflow_state_field;
			jingrow.workflow.avoid_status_override[pagetype] = wf[0].states
				.filter((row) => row.avoid_status_override)
				.map((d) => d.state);
		} else {
			jingrow.workflow.state_fields[pagetype] = null;
		}
	},
	get_state_fieldname: function (pagetype) {
		if (jingrow.workflow.state_fields[pagetype] === undefined) {
			jingrow.workflow.setup(pagetype);
		}
		return jingrow.workflow.state_fields[pagetype];
	},
	get_default_state: function (pagetype, pagestatus) {
		jingrow.workflow.setup(pagetype);
		var value = null;
		$.each(jingrow.workflow.workflows[pagetype].states, function (i, workflow_state) {
			if (cint(workflow_state.pg_status) === cint(pagestatus)) {
				value = workflow_state.state;
				return false;
			}
		});
		return value;
	},
	get_transitions: function (pg) {
		jingrow.workflow.setup(pg.pagetype);
		return jingrow.xcall("jingrow.model.workflow.get_transitions", { pg: pg });
	},
	get_document_state_roles: function (pagetype, state) {
		jingrow.workflow.setup(pagetype);
		let workflow_states =
			jingrow.get_children(jingrow.workflow.workflows[pagetype], "states", { state: state }) ||
			[];
		return workflow_states.map((d) => d.allow_edit);
	},
	is_self_approval_enabled: function (pagetype) {
		return jingrow.workflow.workflows[pagetype].allow_self_approval;
	},
	is_read_only: function (pagetype, name) {
		var state_fieldname = jingrow.workflow.get_state_fieldname(pagetype);
		if (state_fieldname) {
			var pg = locals[pagetype][name];
			if (!pg) return false;
			if (pg.__islocal) return false;

			var state =
				pg[state_fieldname] || jingrow.workflow.get_default_state(pagetype, pg.pagestatus);
			if (!state) return false;

			let allow_edit_roles = jingrow.workflow.get_document_state_roles(pagetype, state);
			let has_common_role = jingrow.user_roles.some((role) =>
				allow_edit_roles.includes(role)
			);
			return !has_common_role;
		}
		return false;
	},
	get_update_fields: function (pagetype) {
		var update_fields = $.unique(
			$.map(jingrow.workflow.workflows[pagetype].states || [], function (d) {
				return d.update_field;
			})
		);
		return update_fields;
	},
	get_state(pg) {
		const state_field = this.get_state_fieldname(pg.pagetype);
		let state = pg[state_field];
		if (!state) {
			state = this.get_default_state(pg.pagetype, pg.pagestatus);
		}
		return state;
	},
	get_all_transitions(pagetype) {
		return jingrow.workflow.workflows[pagetype].transitions || [];
	},
	get_all_transition_actions(pagetype) {
		const transitions = this.get_all_transitions(pagetype);
		return transitions.map((transition) => {
			return transition.action;
		});
	},
};
