<?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 Availability Filter</label>
    <protected>false</protected>
    <values>
        <field>b3s__Javascript_Code__c</field>
        <value xsi:type="xsd:string"><![CDATA[
{
    name: "resource_availability",
    label: "Worker Availability",
    type: 'resource',
    inputs: [
        {
            name: "day",
            type: "date",
            label: "Day",
            required: true,
            //Defaults to today (browser-local) - the morning same-day fill case
            default: (function() {
                const date = new Date();
                return date.getFullYear() + '-' +
                    String(date.getMonth() + 1).padStart(2, '0') + '-' +
                    String(date.getDate()).padStart(2, '0');
            })()
        },
        {
            name: "period",
            type: "select",
            label: "Availability",
            required: true,
            default: "full",
            options: [
                { label: "Morning (before 12pm)", value: "morning" },
                { label: "Day (before 7pm)", value: "day" },
                { label: "Night (after 7pm)", value: "night" },
                { label: "Full day (24h)", value: "full" }
            ]
        },
        {
            name: "mode",
            type: "select",
            label: "Show",
            required: true,
            default: "notUnavailable",
            options: [
                { label: "Not marked unavailable", value: "notUnavailable" },
                { label: "Confirmed available only", value: "confirmed" }
            ]
        }
    ],
    //Hides workers with a non-cancelled shift or an "Unavailable" employee
    //request overlapping the chosen day + period. In "confirmed" mode the
    //worker must also have an "Available" request overlapping that window.
    //Only loaded events are considered - pick a day inside the loaded range.
    matchesItems: function(resource, items) {
        const values = items[0] ?? {};
        const contactId = resource.extendedProps?.record?.Id;
        if (!contactId) return true;
        if (!values.day || !values.period) return true;

        const periods = {
            morning: [0, 12],
            day: [0, 19],
            night: [19, 24],
            full: [0, 24]
        };
        const range = periods[values.period] ?? [0, 24];
        const dateParts = values.day.split('-').map(Number);
        const windowStart = new Date(dateParts[0], dateParts[1] - 1, dateParts[2], range[0], 0, 0, 0);
        const windowEnd = new Date(dateParts[0], dateParts[1] - 1, dateParts[2], range[1], 0, 0, 0);

        let booked = false;
        let confirmed = false;
        for (const event of events) {
            if (!event.start) continue;
            const record = event.extendedProps?.record ?? {};
            if (record.b3s__Contact__c !== contactId) continue;
            //All-day events can come without an end - treat as one day
            const end = event.end ?? new Date(event.start.getTime() + 86400000);
            if (!(event.start < windowEnd && end > windowStart)) continue;

            if (event.title === 'shift' && record.b3s__Status__c !== 'Cancelled') booked = true;
            if (event.title === 'request' && record.b3s__Type__c === 'Unavailable') booked = true;
            if (event.title === 'request' && record.b3s__Type__c === 'Available') confirmed = true;
            if (booked) return false;
        }

        return values.mode === 'confirmed' ? confirmed : true;
    }
}
        ]]></value>
    </values>
    <values>
        <field>b3s__Order__c</field>
        <value xsi:type="xsd:double">10.0</value>
    </values>
</CustomMetadata>
