{"mappings":"AAIA,cAAkC,sBAAsB;AACxD,OAAO,iBAAiB;AAIxB,cAAM;UACJ,MAAM;UACN,SAAS;UACT,QAAQ;UACR,MAAM;;KAGH,wBAAwB;;;;;;;;;;;;;;;;;;;;AAqB7B,cAeM,aAAa,YAAY;;;;;;;;CAQ7B,AACA,MAAM;;;;CAKN,AACA;;;;CAKA,AACA;;;;CAKA,AACA;;;;CAKA,AACA,QAAQ,WAAW,UAAU,YAAY;;;;CAKzC;CAEA,IAAI;CAIJ,UAAU,aAAa,GAAG;CA6B1B;CAgBA,UAAU,UAAU,eAAe;;AAerC,eAAe;AACf,SAAS","names":[],"sources":["../../../../src/web-components/link/component.ts"],"sourcesContent":["import { attr, godown, htmlSlot, styles } from \"@godown/element\";\nimport { property } from \"lit/decorators.js\";\n\nimport Router from \"../router/component.js\";\nimport { css, html, nothing, type TemplateResult } from \"lit\";\nimport GlobalStyle from \"../../internal/global-style.js\";\n\nconst protoName = \"link\";\n\nconst linkTypes = {\n  push: \"push\",\n  replace: \"replace\",\n  normal: \"normal\",\n  auto: \"auto\",\n} as const;\n\ntype LinkType = keyof typeof linkTypes;\n\n/**\n * {@linkcode Link} is used for link jumping, works standalone or in {@linkcode Router}.\n *\n * Set `type` to `\"normal\"`,\n * behave like a normal anchor.\n *\n * Set `type` to `\"push\" `or `\"replace\"`,\n * update history state by `history.pushState` or `history.replaceState`,\n * update all routers whether current pathname is registered or not.\n *\n * Set `type` to `\"auto\"`,\n * only update the routers if the current pathname is registered,\n * if not registered, behave like `\"normal\"`.\n *\n * `replace` property will enforce `history.replaceState`.\n *\n * @fires navigate - Fires when the link is clicked.\n * @category navigation\n */\n@godown(protoName)\n@styles(css`\n  :host {\n    display: inline-block;\n    cursor: default;\n  }\n\n  :host([href]) {\n    cursor: pointer;\n  }\n\n  a {\n    display: contents;\n  }\n`)\nclass Link extends GlobalStyle {\n  /**\n   * If `\"normal\"`, behave like a normal anchor.\n   *\n   * If `\"auto\"` or `\"push\"`, call `history.pushState` if `replace` is false,\n   *\n   * If `\"replace\"`, call `history.replaceState`.\n   */\n  @property()\n  type: LinkType = linkTypes.auto;\n\n  /**\n   * If `true`, the {@linkcode Router} will not be updated.\n   */\n  @property({ type: Boolean })\n  suppress = false;\n\n  /**\n   * Use `replaceState` instead of `pushState`.\n   */\n  @property({ type: Boolean })\n  replace = false;\n\n  /**\n   * A element href.\n   */\n  @property()\n  href: string;\n\n  /**\n   * A element target.\n   */\n  @property()\n  target: \"_blank\" | \"_self\" | \"_parent\" | \"_top\" = \"_self\";\n\n  /**\n   * Location state object.\n   */\n  state = {};\n\n  get pathname(): string {\n    return new URL(this.href, location.href).pathname;\n  }\n\n  protected _handleClick(e: MouseEvent): void {\n    const { state, type, href, pathname, suppress } = this;\n    if (!href) {\n      return;\n    }\n    this.dispatchCustomEvent(\"navigate\", {\n      ...this.observedRecord,\n      pathname,\n      state,\n    });\n    if (href.startsWith(\"#\") || type === linkTypes.normal) {\n      return;\n    }\n    this.handleState();\n    const routers = [...Router.routerInstances];\n    if (\n      // only runs when suppress is false\n      !suppress &&\n      (type !== linkTypes.auto ||\n        // in auto mode, only update the routers if the current pathname is registered\n        routers.some((i) => i.search(location.pathname)))\n    ) {\n      e.preventDefault();\n      Router.routerInstances.forEach((i) => {\n        i.handlePopstate();\n      });\n    }\n  }\n\n  handleState: () => void = () => {\n    switch (this.type) {\n      case linkTypes.auto:\n      case linkTypes.push:\n        if (!this.replace) {\n          // type is auto or push and replace is false\n          history.pushState(this.state, \"\", this.href);\n          break;\n        }\n      // fallthrough to replace\n      case linkTypes.replace:\n        history.replaceState(this.state, \"\", this.href);\n        break;\n    }\n  };\n\n  protected render(): TemplateResult<1> {\n    return html`\n      <a\n        part=\"root\"\n        ${attr(this.observedRecord)}\n        href=\"${this.href || nothing}\"\n        target=\"${this.target}\"\n        @click=${this._handleClick}\n      >\n        ${htmlSlot()}\n      </a>\n    `;\n  }\n}\n\nexport default Link;\nexport { Link };\n"],"version":3,"file":"component.d.ts"}