import type { Resource, TemplateResultOrSymbol } from "@src/tems"; import { LitElement, nothing } from "lit"; import * as rdf from "@src/rdf"; import { property } from "lit/decorators.js"; const rdfs = Object.values(rdf); export class TemsObjectsHandler extends LitElement { @property({ attribute: false }) objects?: Resource[] = []; hasType = (type: string, objs: Resource[] = this.objects as Resource[]) => { const typeValue = new Set(objs.flatMap((o) => o["@type"])); return typeValue.has(type); }; renderTemplateWhenWith( requiredProperties: (string | string[])[], template: () => TemplateResultOrSymbol, objs: Resource[] = this.objects as Resource[] ): TemplateResultOrSymbol { const tester = ( property: string | string[], cb: (some: string) => boolean ) => Array.isArray(property) ? property.some((subProp) => cb(subProp)) : cb(property); const typeProperties = requiredProperties.filter((property) => tester(property, (p) => rdfs.includes(p)) ); if ( typeProperties.every((property) => tester(property, (p) => this.hasType(p, objs)) ) ) { return template.call(this); } return nothing; } }