<?xml version="1.0" encoding="UTF-8"?>
<CustomMetadata xmlns="http://soap.sforce.com/2006/04/metadata" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <label>Resource Available After Filter</label>
    <protected>false</protected>
    <values>
        <field>b3s__Javascript_Code__c</field>
        <value xsi:type="xsd:string"><![CDATA[
{
    name: "resource_available_after",
    label: "Available After Time",
    type: 'resource',
    inputs: [
        {
            name: "time",
            type: "time",
            label: "Available after",
            required: true
        }
    ],
    //A worker is available after the entered time if none of their loaded
    //shifts (non-cancelled) or "Unavailable" employee requests overlap the
    //window from that time to midnight on the event's own day.
    matchesItems: function(resource, items) {
        const values = items[0] ?? {};
        const contactId = resource.extendedProps?.record?.Id;
        if (!contactId) return true;
        if (!values.time) return true;

        const parts = values.time.split(':');
        const hours = Number(parts[0]);
        const minutes = Number(parts[1] ?? 0);

        const booked = events.some((event) => {
            if (!event.start || !event.end) return false;
            const record = event.extendedProps?.record ?? {};
            if (record.b3s__Contact__c !== contactId) return false;

            const isShift = event.title === 'shift' && record.b3s__Status__c !== 'Cancelled';
            const isUnavailable = event.title === 'request' && record.b3s__Type__c === 'Unavailable';
            if (!isShift && !isUnavailable) return false;

            const windowStart = new Date(event.start);
            windowStart.setHours(hours, minutes, 0, 0);
            const windowEnd = new Date(event.start);
            windowEnd.setHours(24, 0, 0, 0);

            return event.start < windowEnd && event.end > windowStart;
        });

        return !booked;
    }
}
        ]]></value>
    </values>
    <values>
        <field>b3s__Order__c</field>
        <value xsi:type="xsd:double">8.0</value>
    </values>
</CustomMetadata>
