"use strict";
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var link_exports = {};
__export(link_exports, {
  Link: () => Link,
  LinkDefinition: () => LinkDefinition,
  validateElementRef: () => validateElementRef
});
module.exports = __toCommonJS(link_exports);
var import_scroll_into_view_if_needed = __toESM(require("scroll-into-view-if-needed"));
var import_controls = require("@makeswift/controls");
function validateElementRef(href) {
  try {
    const hash = new URL(`http://www.example.com/${href}`).hash;
    return href === hash ? hash : void 0;
  } catch (error) {
    return void 0;
  }
}
class LinkDefinition extends import_controls.LinkDefinition {
  static deserialize(data) {
    if (data.type !== LinkDefinition.type) {
      throw new Error(`Link: expected type ${LinkDefinition.type}, got ${data.type}`);
    }
    const { config } = LinkDefinition.schema.definition.parse(data);
    return Link(config);
  }
  resolveOnClick(data, href, scrollOptions) {
    const onClick = (event) => {
      if (event.defaultPrevented)
        return;
      if (data && data.type === "SCROLL_TO_ELEMENT") {
        const hash = validateElementRef(href);
        if (hash == null) {
          console.error(`Scroll-to-element link received invalid href: ${href}`);
          return;
        }
        event.preventDefault();
        const view = event.view;
        (0, import_scroll_into_view_if_needed.default)(view.document.querySelector(hash), {
          behavior: "smooth",
          block: scrollOptions?.block
        });
        if (view.location.hash !== hash)
          view.history.pushState({}, "", hash);
      }
    };
    onClick.$scrollOptions = scrollOptions;
    return onClick;
  }
}
function Link(config) {
  return new LinkDefinition(config ?? {});
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  Link,
  LinkDefinition,
  validateElementRef
});
//# sourceMappingURL=link.js.map