'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

const index$1 = require('./index-140bcbcd.js');
const index = require('./index-b7de914b.js');
require('./_commonjsHelpers-537d719a.js');

function month(date) {
  const d = new Date(date);
  d.setDate(1);
  let day = -((d.getDay() + 6) % 7) + 1;
  const result = [];
  for (let row = 0; row < 6; row++) {
    const r = [];
    for (let column = 0; column < 7; column++) {
      const d = new Date(date);
      d.setDate(day++);
      r.push(index.dist.Date.create(d));
    }
    if (row < 5 || index.dist.Date.firstOfMonth(r[0]) == index.dist.Date.firstOfMonth(date))
      result.push(r);
  }
  return result;
}
function weekdays() {
  const day = new globalThis.Date();
  day.setDate(day.getDate() - day.getDay() + 1);
  const result = [];
  for (let i = 0; i < 7; i++) {
    result.push(day.toLocaleString(undefined, { weekday: "short" }));
    day.setDate(day.getDate() + 1);
  }
  return result;
}
function months(current) {
  const day = new globalThis.Date(current);
  const result = [];
  for (let i = 0; i < 12; i++) {
    day.setMonth(i);
    const date = index.dist.Date.create(day);
    result.push({
      date,
      name: day.toLocaleString(undefined, { month: "long" }),
      selected: date == current,
    });
  }
  return result;
}
function years(current) {
  const day = new globalThis.Date(current);
  const start = new Date().getFullYear() - 10;
  const end = new Date().getFullYear() + 10;
  const result = [];
  for (let i = start; i <= end; i++) {
    day.setFullYear(i);
    const date = index.dist.Date.create(day);
    result.push({
      date,
      name: day.toLocaleString(undefined, { year: "numeric" }),
      selected: date == current,
    });
  }
  return result;
}

const styleCss$3 = ".sc-smoothly-calendar-h{display:block;--other-month-opacity:0.5}.sc-smoothly-calendar-h>smoothly-input-month.sc-smoothly-calendar{width:calc(100% - 1em);padding:0.5em 0.5em 0 0.5em}th.sc-smoothly-calendar,td.sc-smoothly-calendar{text-align:center;padding:0.5em;min-width:2em;background-color:rgb(var(--smoothly-default-shade));cursor:pointer;-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none}td.currentMonth.sc-smoothly-calendar{color:rgb(var(--smoothly-default-contrast))}td.sc-smoothly-calendar:not(.currentMonth){color:rgba(var(--smoothly-default-contrast), var(--other-month-opacity))}td.sc-smoothly-calendar:nth-child(6):not(.currentMonth).selected,td.sc-smoothly-calendar:nth-child(6):not(.currentMonth).dateRange,td.sc-smoothly-calendar:nth-child(7):not(.currentMonth).selected,td.sc-smoothly-calendar:nth-child(7):not(.currentMonth).dateRange{color:rgba(var(--smoothly-default-contrast))}td.sc-smoothly-calendar:nth-child(6),td.sc-smoothly-calendar:nth-child(7){color:rgb(var(--smoothly-danger-tint))}td.sc-smoothly-calendar:nth-child(6):not(.currentMonth),td.sc-smoothly-calendar:nth-child(7):not(.currentMonth){color:rgba(var(--smoothly-danger-tint), var(--other-month-opacity))}td.sc-smoothly-calendar:not(.selected):hover{color:rgb(var(--smoothly-primary-contrast));background:rgb(var(--smoothly-primary-tint))}td.selected.sc-smoothly-calendar{color:rgb(var(--smoothly-primary-contrast));background:rgb(var(--smoothly-primary-color))}td.sc-smoothly-calendar:not(.selected,.dateRange).sc-smoothly-calendar:not(:hover).today{background:rgb(var(--smoothly-dark-tint));color:rgb(var(--smoothly-dark-contrast))}td.dateRange.sc-smoothly-calendar{color:rgb(var(--smoothly-primary-contrast));background:rgb(var(--smoothly-primary-tint))}";

const Calendar = class {
  constructor(hostRef) {
    index$1.registerInstance(this, hostRef);
    this.valueChanged = index$1.createEvent(this, "valueChanged", 7);
    this.startChanged = index$1.createEvent(this, "startChanged", 7);
    this.endChanged = index$1.createEvent(this, "endChanged", 7);
    this.value = index.dist.Date.now();
    this.clickCounter = 0;
  }
  onStart(next) {
    this.startChanged.emit(next);
  }
  onEnd(next) {
    this.endChanged.emit(next);
  }
  render() {
    var _a, _b;
    return [
      index$1.h("smoothly-input-month", { value: (_a = this.month) !== null && _a !== void 0 ? _a : this.value, onValueChanged: event => {
          this.month = event.detail;
          event.stopPropagation();
        } }),
      index$1.h("table", null, index$1.h("thead", null, index$1.h("tr", null, weekdays().map(day => (index$1.h("th", null, day))))), month((_b = this.month) !== null && _b !== void 0 ? _b : this.value).map(week => (index$1.h("tr", null, week.map(date => {
        var _a, _b, _c;
        return (index$1.h("td", { tabindex: 1, onClick: () => {
            this.valueChanged.emit((this.value = date));
            this.clickCounter += 1;
            if (this.clickCounter % 2 == 1)
              this.start = this.end = date;
            else {
              if (this.start && date > this.start)
                this.end = date;
              else
                this.start = date;
            }
          }, class: (date == this.value ? ["selected"] : [])
            .concat(...(date == index.dist.Date.now() ? ["today"] : []), index.dist.Date.firstOfMonth((_a = this.month) !== null && _a !== void 0 ? _a : this.value) == index.dist.Date.firstOfMonth(date) ? ["currentMonth"] : [], this.doubleInput
            ? index.dist.Date.parse(date) >= index.dist.Date.parse((_b = this.start) !== null && _b !== void 0 ? _b : "") &&
              index.dist.Date.parse(date) <= index.dist.Date.parse((_c = this.end) !== null && _c !== void 0 ? _c : "")
              ? ["dateRange"]
              : []
            : "")
            .join(" ") }, date.substring(8, 10)));
      }))))),
    ];
  }
  get element() { return index$1.getElement(this); }
  static get watchers() { return {
    "start": ["onStart"],
    "end": ["onEnd"]
  }; }
};
Calendar.style = styleCss$3;

const styleCss$2 = ".sc-smoothly-input-month-h{display:-ms-flexbox;display:flex;-ms-flex-pack:justify;justify-content:space-between;font-size:large}smoothly-icon.sc-smoothly-input-month{font-size:smaller}";

const MonthSelector = class {
  constructor(hostRef) {
    index$1.registerInstance(this, hostRef);
    this.valueChanged = index$1.createEvent(this, "valueChanged", 7);
  }
  onValueChanged(next) {
    this.valueChanged.emit(next);
  }
  adjustMonth(delta) {
    var _a;
    const date = index.dist.Date.parse((_a = this.value) !== null && _a !== void 0 ? _a : index.dist.Date.now());
    date.setMonth(date.getMonth() + delta);
    this.value = index.dist.Date.create(date);
  }
  render() {
    var _a, _b;
    return [
      index$1.h("div", { onClick: () => this.adjustMonth(-1) }, index$1.h("smoothly-icon", { name: "chevron-back-outline", size: "small" })),
      index$1.h("smoothly-selector", { onSelected: (e) => (this.value = e.detail) }, years((_a = this.value) !== null && _a !== void 0 ? _a : index.dist.Date.now()).map(year => (index$1.h("smoothly-item", { value: year.date, selected: year.selected }, year.name)))),
      index$1.h("smoothly-selector", { onSelected: (e) => (this.value = e.detail) }, months((_b = this.value) !== null && _b !== void 0 ? _b : index.dist.Date.now()).map(month => (index$1.h("smoothly-item", { value: month.date, selected: month.selected }, month.name)))),
      index$1.h("div", { onClick: () => this.adjustMonth(1) }, index$1.h("smoothly-icon", { name: "chevron-forward-outline", size: "small" })),
    ];
  }
  static get watchers() { return {
    "value": ["onValueChanged"]
  }; }
};
MonthSelector.style = styleCss$2;

const styleCss$1 = "[selected].sc-smoothly-item-h{background-color:rgb(var(--smoothly-primary-shade));color:rgb(var(--smoothly-primary-contrast))}.sc-smoothly-item-h{padding:.5em;cursor:pointer;background-color:rgb(var(--smoothly-default-shade));color:rgb(var(--smoothly-default-contrast));border:rgb(var(--smoothly-default-shade) solid 1px)}.sc-smoothly-item-h:hover{background-color:rgb(var(--smoothly-primary-color));color:rgb(var(--smoothly-primary-contrast))}";

const Item = class {
  constructor(hostRef) {
    index$1.registerInstance(this, hostRef);
    this.itemSelected = index$1.createEvent(this, "itemSelected", 7);
  }
  onSelectedChanged(value, old) {
    if (value && !old)
      this.itemSelected.emit();
  }
  onClick() {
    this.selected = true;
    this.itemSelected.emit();
  }
  async filter(filter) {
    const result = !(this.element.hidden = filter
      ? !((typeof this.value === "string" ? this.value : JSON.stringify(this.value)).toLowerCase().includes(filter) ||
        this.element.innerText.toLowerCase().includes(filter))
      : false);
    return result;
  }
  render() {
    return index$1.h("slot", null);
  }
  get element() { return index$1.getElement(this); }
  static get watchers() { return {
    "selected": ["onSelectedChanged"]
  }; }
};
Item.style = styleCss$1;

const styleCss = ".sc-smoothly-selector-h{position:relative;height:100%}.sc-smoothly-selector-h>main.sc-smoothly-selector,.sc-smoothly-selector-h>main.sc-smoothly-selector>*.sc-smoothly-selector{height:100%;display:-ms-flexbox;display:flex;-ms-flex-align:center;align-items:center}.sc-smoothly-selector-h>div.sc-smoothly-selector>nav.sc-smoothly-selector{display:-ms-flexbox;display:flex;-ms-flex-direction:column;flex-direction:column;position:absolute;z-index:10}.sc-smoothly-selector-h>aside.sc-smoothly-selector{position:absolute;top:0;left:5em;z-index:10;background-color:rgb(var(--smoothly-tertiary-color));color:rgb(var(--smoothly-tertiary-contrast));height:20px;border-radius:5px;padding:5px;display:-ms-flexbox;display:flex;-ms-flex-align:center;align-items:center}.missing.sc-smoothly-selector-h>aside.sc-smoothly-selector{background-color:rgb(var(--smoothly-warning-color));color:rgb(var(--smoothly-warning-contast))}.hidden.sc-smoothly-selector{display:none}.sc-smoothly-selector-h>section.sc-smoothly-selector{position:fixed;top:0px;left:0px;width:100vw;height:100vh;z-index:10}";

const Selector = class {
  constructor(hostRef) {
    index$1.registerInstance(this, hostRef);
    this.selected = index$1.createEvent(this, "selected", 7);
    this.opened = false;
    this.items = [];
    this.missing = false;
    this.filter = "";
  }
  onSelectedChange(value, old) {
    if (old)
      old.selected = false;
    this.selected.emit(value === null || value === void 0 ? void 0 : value.value);
  }
  async onFilterChange(value) {
    value = value.toLowerCase();
    if (!(await Promise.all(this.items.map(item => item.filter(value)))).some(r => r)) {
      this.missing = true;
      this.items.forEach(el => el.filter(""));
    }
    else
      this.missing = false;
  }
  onClick(event) {
    event.stopPropagation();
    this.opened = !this.opened;
  }
  onItemSelected(event) {
    this.selectedElement = event.target;
    if (this.mainElement)
      this.mainElement.innerHTML = this.selectedElement.innerHTML;
  }
  onKeyDown(event) {
    event.stopPropagation();
    if (this.opened) {
      let direction = 0;
      switch (event.key) {
        case "ArrowUp":
          direction = -1;
          break;
        case "ArrowDown":
          direction = 1;
          break;
        case "Escape":
          this.filter = "";
          break;
        case "Backspace":
          this.filter = this.filter.slice(0, -1);
          break;
        case "Enter":
          this.items.forEach(element => {
            if (!element.hidden) {
              this.selected.emit(element.value);
              if (this.mainElement && this.selectedElement)
                this.mainElement.innerHTML = this.selectedElement.innerHTML;
              this.opened = false;
              this.filter = "";
            }
          });
          break;
        default:
          if (event.key.length == 1)
            this.filter += event.key;
          break;
      }
      this.move(direction);
    }
    else if (event.key == "Enter")
      this.opened = true;
  }
  move(direction) {
    if (direction) {
      let selectedIndex = this.items.findIndex(item => item == this.selectedElement);
      console.log("selectedindex", selectedIndex);
      do {
        selectedIndex = (selectedIndex + direction + this.items.length) % this.items.length;
      } while (this.items[selectedIndex].hidden);
      this.selectedElement = this.items[selectedIndex];
      this.selectedElement.selected = true;
    }
  }
  render() {
    return (index$1.h(index$1.Host, { tabIndex: 2, class: this.missing ? "missing" : "" }, index$1.h("main", { ref: element => (this.mainElement = element) }, "(none)"), this.filter.length != 0 ? (index$1.h("aside", { ref: element => (this.aside = element) }, this.filter, index$1.h("button", { onClick: e => {
        e.stopPropagation();
        this.filter = "";
      } }, index$1.h("smoothly-icon", { name: "close", size: "small" })))) : undefined, this.opened ? index$1.h("section", { onClick: () => (this.opened = true) }) : [], index$1.h("div", { class: this.opened ? "" : "hidden" }, index$1.h("nav", null, index$1.h("slot", null)))));
  }
  componentDidRender() {
    const items = [];
    const children = this.element.querySelectorAll("div > nav > smoothly-item");
    for (let i = 0; i < children.length; i++) {
      const node = children.item(i);
      if (isItem(node))
        items.push(node);
    }
    this.items = items;
  }
  get element() { return index$1.getElement(this); }
  static get watchers() { return {
    "selectedElement": ["onSelectedChange"],
    "filter": ["onFilterChange"]
  }; }
};
function isItem(value) {
  return (typeof value == "object" &&
    (typeof value.selected == "boolean" || value.selected == undefined) &&
    typeof value.filter == "function");
}
Selector.style = styleCss;

exports.smoothly_calendar = Calendar;
exports.smoothly_input_month = MonthSelector;
exports.smoothly_item = Item;
exports.smoothly_selector = Selector;
