import { h } from "@stencil/core";
import { getCalendarDays, parseDate, addMonths, MONTH_NAMES, WEEK_DAY_NAMES, } from "../../utils/date-helpers";
/**
 * Calendar 日历组件
 * 月份视图日历，支持选中日期与月份切换
 */
export class Calendar {
    /**
     * 绑定值（ISO 日期字符串）
     */
    modelValue;
    /**
     * 每周第一天（0=周日，1=周一 …）
     * @default 0
     */
    firstDayOfWeek = 0;
    /**
     * 禁用日期判定（声明预留，暂不强求实现）
     */
    disabledDate;
    /**
     * 值改变事件，detail 为 ISO 日期字符串
     */
    hbChange;
    viewDate = new Date();
    selectedDate;
    componentDidLoad() {
        this.initFromModelValue();
    }
    handleModelValueChange() {
        this.initFromModelValue();
    }
    initFromModelValue() {
        const parsed = parseDate(this.modelValue);
        this.selectedDate = parsed;
        if (parsed) {
            this.viewDate = new Date(parsed.getFullYear(), parsed.getMonth(), 1);
        }
    }
    handleSelect = (day) => {
        if (!day.isCurrentMonth)
            return;
        if (this.disabledDate && this.disabledDate(day.date))
            return;
        this.selectedDate = day.date;
        const iso = this.toISODate(day.date);
        this.modelValue = iso;
        this.hbChange.emit(iso);
    };
    toISODate(date) {
        const y = date.getFullYear();
        const m = String(date.getMonth() + 1).padStart(2, '0');
        const d = String(date.getDate()).padStart(2, '0');
        return `${y}-${m}-${d}`;
    }
    changeMonth(delta) {
        this.viewDate = addMonths(this.viewDate, delta);
    }
    /** 按 firstDayOfWeek 重排星期标题（getCalendarDays 网格恒以周日为首列） */
    get orderedWeekNames() {
        const offset = ((this.firstDayOfWeek % 7) + 7) % 7;
        return [...WEEK_DAY_NAMES.slice(offset), ...WEEK_DAY_NAMES.slice(0, offset)];
    }
    render() {
        const days = getCalendarDays(this.viewDate, this.selectedDate);
        const current = this.viewDate;
        return (h("div", { key: '349964b635ebec88f000ea4b26c4c6779f9f8925', class: "hb-calendar" }, h("div", { key: '05a23a21700199202f902cab89c01af76ebe4bb6', class: "hb-calendar__header" }, h("button", { key: '442c8a247cff4cbfc76e1a73a983f6e1255f7fd5', type: "button", class: "hb-calendar__prev-btn", onClick: () => this.changeMonth(-1), "aria-label": "\u4E0A\u4E00\u6708" }, "\u2039"), h("span", { key: 'be82bfda5eddbee738587405924ebad7cb4757f9', class: "hb-calendar__header-label" }, current.getFullYear(), "\u5E74 ", MONTH_NAMES[current.getMonth()]), h("button", { key: 'd25451367e0ae14dd93efe57bfa7381535940d4b', type: "button", class: "hb-calendar__next-btn", onClick: () => this.changeMonth(1), "aria-label": "\u4E0B\u4E00\u6708" }, "\u203A")), h("div", { key: '1ecc238f1f6c08e4e32aad8067277dd4f6ee94b8', class: "hb-calendar__body" }, h("div", { key: '52e7bfd36efbcb3cf0d0ee5f5f8a2798169f9a64', class: "hb-calendar__weekdays" }, this.orderedWeekNames.map((day) => (h("div", { class: "hb-calendar__weekday" }, day)))), h("div", { key: '54545d72d643142c0b3f46f2b6817764fe22fdb2', class: "hb-calendar__days" }, days.map((day, index) => (h("div", { key: index, class: {
                'hb-calendar__day': true,
                'hb-calendar__day--other-month': !day.isCurrentMonth,
                'hb-calendar__day--today': day.isToday,
                'hb-calendar__day--selected': day.isSelected,
            }, onClick: () => this.handleSelect(day) }, day.date.getDate())))))));
    }
    static get is() { return "hb-calendar"; }
    static get encapsulation() { return "shadow"; }
    static get originalStyleUrls() {
        return {
            "$": ["calendar.css"]
        };
    }
    static get styleUrls() {
        return {
            "$": ["calendar.css"]
        };
    }
    static get properties() {
        return {
            "modelValue": {
                "type": "string",
                "mutable": true,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": true,
                "docs": {
                    "tags": [],
                    "text": "\u7ED1\u5B9A\u503C\uFF08ISO \u65E5\u671F\u5B57\u7B26\u4E32\uFF09"
                },
                "getter": false,
                "setter": false,
                "attribute": "model-value",
                "reflect": false
            },
            "firstDayOfWeek": {
                "type": "number",
                "mutable": false,
                "complexType": {
                    "original": "number",
                    "resolved": "number",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [{
                            "name": "default",
                            "text": "0"
                        }],
                    "text": "\u6BCF\u5468\u7B2C\u4E00\u5929\uFF080=\u5468\u65E5\uFF0C1=\u5468\u4E00 \u2026\uFF09"
                },
                "getter": false,
                "setter": false,
                "attribute": "first-day-of-week",
                "reflect": false,
                "defaultValue": "0"
            },
            "disabledDate": {
                "type": "unknown",
                "mutable": false,
                "complexType": {
                    "original": "(date: Date) => boolean",
                    "resolved": "(date: Date) => boolean",
                    "references": {
                        "Date": {
                            "location": "global",
                            "id": "global::Date"
                        }
                    }
                },
                "required": false,
                "optional": true,
                "docs": {
                    "tags": [],
                    "text": "\u7981\u7528\u65E5\u671F\u5224\u5B9A\uFF08\u58F0\u660E\u9884\u7559\uFF0C\u6682\u4E0D\u5F3A\u6C42\u5B9E\u73B0\uFF09"
                },
                "getter": false,
                "setter": false
            }
        };
    }
    static get states() {
        return {
            "viewDate": {},
            "selectedDate": {}
        };
    }
    static get events() {
        return [{
                "method": "hbChange",
                "name": "hbChange",
                "bubbles": true,
                "cancelable": true,
                "composed": true,
                "docs": {
                    "tags": [],
                    "text": "\u503C\u6539\u53D8\u4E8B\u4EF6\uFF0Cdetail \u4E3A ISO \u65E5\u671F\u5B57\u7B26\u4E32"
                },
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                }
            }];
    }
    static get watchers() {
        return [{
                "propName": "modelValue",
                "methodName": "handleModelValueChange"
            }];
    }
}
//# sourceMappingURL=Calendar.js.map
