<template>
	<div class="box">
		<h3 class="title-configs">House rules</h3>
		<v-row class="text-center">
			<v-col
				v-for="item in rules()"
				:key="item.id"
				cols="12"
				lg="4"
				md="4"
				sm="6"
			>
				<v-icon
					:class="item.icon?.class || 'text-success'"
					:icon="item.icon ? `mdi-${item.icon.name}` : 'mdi-check'"
				/>
				<span>{{ item.text }}</span>
			</v-col>
		</v-row>
	</div>
</template>

<script setup>
const props = defineProps({ property: { type: Object, default: () => ({}) } });
const propertyData = Vue.ref();
propertyData.value = window.useProperty(props.property);

const rules = () => {
	const rules = [];

	if (propertyData.value.property_configurations) {
		if (propertyData.value.property_configurations.hour_check_in) {
			const checkinTime = moment(
				propertyData.value.property_configurations.hour_check_in,
				"HH:mm"
			);

			rules.push({
				text: `Check-in is at ${
					checkinTime ? checkinTime.format("hh:mm A") : ""
				}`,
			});
		}

		if (propertyData.value.property_configurations.hour_check_out) {
			const checkoutTime = moment(
				propertyData.value.property_configurations.hour_check_out,
				"HH:mm"
			);

			rules.push({
				text: `Check-out by ${
					checkoutTime ? checkoutTime.format("hh:mm A") : ""
				}`,
			});
		}
	}

	if (propertyData.value?.property_house_rules?.length) {
		const smoking_rules = [157, 158, 407];
		const pets_includes_id = 159;
		const pets_not_includes_id = 160;
		const pets_rules = [pets_includes_id, pets_not_includes_id];

		if (
			!propertyData.value?.property_house_rules.some((el) =>
				pets_rules.includes(el.id)
			)
		) {
			rules.push({
				icon: {
					name: "close",
					class: "text-black",
				},
				text: "Pets not allowed",
			});
		} else if (
			propertyData.value?.property_house_rules.some(
				(el) => pets_includes_id === el.id
			)
		) {
			const pet_rule = propertyData.value?.property_house_rules.find(
				(el) => pets_includes_id === el.id
			);
			rules.push({
				icon: {
					name: "check",
					class: "text-success",
				},
				text: pet_rule.name,
			});
		} else {
			const pet_rule = propertyData.value?.property_house_rules.find(
				(el) => pets_not_includes_id === el.id
			);
			rules.push({
				icon: {
					name: "close",
					class: "text-red",
				},
				text: pet_rule.name,
			});
		}
		if (
			!propertyData.value?.property_house_rules.some((el) =>
				smoking_rules.includes(el.id)
			)
		) {
			rules.push({
				icon: {
					name: "close",
					class: "text-red",
				},
				text: "Smoking or Vaping not allowed",
			});
		}

		for (
			let i = 0;
			i < propertyData.value?.property_house_rules.length;
			i++
		) {
			const rule = propertyData.value?.property_house_rules[i];
			if (smoking_rules.includes(rule.id) || pets_rules.includes(rule.id))
				continue;
			const is_no_rule = rule.name.toLowerCase().search("no") >= 0;
			rules.push({
				icon: {
					name: is_no_rule ? "close" : "check",
					class: is_no_rule ? "text-red" : "text-success",
				},
				text: rule.name,
			});
		}

		// const rules_set = new Set(
		//     propertyData.value.property_house_rules.map((el) => el.id)
		// )
		// for (const id in rules_set) {
		//   const rule = this.amenities_house_rules.find(
		//       (el) => el.id == id
		//   )
		//   if (!rule || pets_rules.includes(id)) continue
		//   const is_no_rule = rule.name.toLowerCase().search('no') >= 0
		//   rules.push({
		//     icon: {
		//       name: is_no_rule ? 'times' : 'check',
		//       class: is_no_rule ? 'text-danger' : 'text-success',
		//     },
		//     text: rule.name,
		//   })
		// }
		// propertyData.value.property_house_rules.forEach((el) => {
		// 	const is_no_rule = el.name.toLowerCase().search('no') >= 0
		// 	rules.push({
		// 		icon: {
		// 			name: is_no_rule ? 'times' : 'check',
		// 			class: is_no_rule ? 'text-danger' : 'text-success',
		// 		},
		// 		text: el.name,
		// 	})
		// })
	}

	// if (propertyData.value?.property_amenities?.length) {
	// 	const smoking_rules = [157, 158]
	// 	const pets_rules = [159]

	// 	if (
	// 		!propertyData.value?.property_amenities.some((el) =>
	// 			pets_rules.includes(el.id)
	// 		)
	// 	) {
	// 		rules.push({
	// 			icon: {
	// 				name: 'times',
	// 				class: 'text-danger',
	// 			},
	// 			text: 'Pets not allowed',
	// 		})
	// 	}
	// 	if (
	// 		!propertyData.value?.property_amenities.some((el) =>
	// 			smoking_rules.includes(el.id)
	// 		)
	// 	) {
	// 		rules.push({
	// 			icon: {
	// 				name: 'times',
	// 				class: 'text-danger',
	// 			},
	// 			text: 'Smoking not allowed',
	// 		})
	// 	}
	// }

	if (propertyData.value?.properties?.age_restriction_active) {
		rules.push({
			icon: {
				name: "account-off",
				class: "text-red",
			},
			text: `Booking Guest Age Restriction: ${propertyData.value?.properties?.age_restriction}+`,
		});
	}

	return rules;
};
</script>
