import { memo, useEffect, useState } from "@wordpress/element";
import InspectorControl from "../../components/inspectorControls/inspectorControls";
import { DownArrow, panelBodyRightIcon, panelBodyTitleIcon } from "../../icons/icons";
import Inspector from "./inspect";
import classNames from "classnames";
const Render = ({ attributes, setAttributes, allTaxonomies }) => {
const [dropdownOpen, setDropdown] = useState(false);
const [selectTerm, setSelectTerm] = useState("All");
const [searchValue, setSearchValue] = useState("");
attributes.allTaxonomies = allTaxonomies;
const {
uniqueId,
taxonomyType,
selectTermsType,
selectedTerms,
excludeTerms,
categoryLabel,
overrideCategoryLabel,
allTextLabel,
showPostCount,
searchInDropdown,
filterType,
searchPlaceholderText,
titleGlobalTypography,
optionGlobalTypography,
} = attributes;
const [activeBtn, setActiveBtn] = useState(allTextLabel);
useEffect(() => {
const clickOutSite = (e) => {
const target = e.target.closest(".sp-smart-post-taxonomy-filter");
if (!target && dropdownOpen) {
setDropdown(false);
}
};
window.addEventListener("click", clickOutSite);
return () => window.removeEventListener("click", clickOutSite);
});
let allTerms = [];
if ("all" === selectTermsType) {
allTaxonomies?.forEach((taxonomy) => {
if (taxonomyType === taxonomy.name) {
allTerms = [...allTerms, ...taxonomy.terms_items];
}
});
}
if ("exclude" === selectTermsType) {
const excludeTermIds = excludeTerms.map((term) => term.value);
allTaxonomies?.forEach((taxonomy) => {
if (taxonomyType === taxonomy.name) {
allTerms = [...allTerms, ...taxonomy.terms_items];
}
});
allTerms = allTerms.filter((term) => !excludeTermIds.includes(term.value));
}
if ("specific" === selectTermsType) {
allTerms = selectedTerms;
}
const setTaxonomy = (e) => {
const termValue = e.target.closest("a").dataset.value;
setActiveBtn(termValue);
setSelectTerm(termValue);
setDropdown(false);
};
useEffect(() => {
setActiveBtn("all");
}, [taxonomyType, selectTermsType]);
const termLabel = allTerms?.find((term) => term.value === selectTerm);
if (searchValue) {
allTerms = allTerms.filter((term) => term.label.toLowerCase().includes(searchValue.toLowerCase()));
}
return (
<>