"use strict";
var __webpack_require__ = {};
(()=>{
    __webpack_require__.n = (module)=>{
        var getter = module && module.__esModule ? ()=>module['default'] : ()=>module;
        __webpack_require__.d(getter, {
            a: getter
        });
        return getter;
    };
})();
(()=>{
    __webpack_require__.d = (exports1, definition)=>{
        for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
            enumerable: true,
            get: definition[key]
        });
    };
})();
(()=>{
    __webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
})();
(()=>{
    __webpack_require__.r = (exports1)=>{
        if ("u" > typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
            value: 'Module'
        });
        Object.defineProperty(exports1, '__esModule', {
            value: true
        });
    };
})();
var __webpack_exports__ = {};
__webpack_require__.r(__webpack_exports__);
__webpack_require__.d(__webpack_exports__, {
    AtomicResultPrintableUri: ()=>AtomicResultPrintableUri
});
const bueno_namespaceObject = require("@coveo/bueno");
const headless_namespaceObject = require("@coveo/headless");
const external_lit_namespaceObject = require("lit");
const decorators_js_namespaceObject = require("lit/decorators.js");
const when_js_namespaceObject = require("lit/directives/when.js");
const attributes_slot_cjs_namespaceObject = require("../../common/item-link/attributes-slot.cjs");
const item_link_cjs_namespaceObject = require("../../common/item-link/item-link.cjs");
const validate_props_controller_cjs_namespaceObject = require("../../common/validate-props-controller/validate-props-controller.cjs");
const result_context_controller_cjs_namespaceObject = require("../result-template-component-utils/context/result-context-controller.cjs");
const binding_guard_cjs_namespaceObject = require("../../../decorators/binding-guard.cjs");
const bindings_cjs_namespaceObject = require("../../../decorators/bindings.cjs");
const error_guard_cjs_namespaceObject = require("../../../decorators/error-guard.cjs");
const light_dom_cjs_namespaceObject = require("../../../mixins/light-dom.cjs");
const utils_cjs_namespaceObject = require("../../../utils/utils.cjs");
const arrow_right_cjs_namespaceObject = require("../../../images/arrow-right.cjs");
var arrow_right_cjs_default = /*#__PURE__*/ __webpack_require__.n(arrow_right_cjs_namespaceObject);
require("../atomic-result-text/atomic-result-text.cjs");
require("../../common/atomic-icon/atomic-icon.cjs");
const external_atomic_result_printable_uri_tw_css_cjs_namespaceObject = require("./atomic-result-printable-uri.tw.css.cjs");
var external_atomic_result_printable_uri_tw_css_cjs_default = /*#__PURE__*/ __webpack_require__.n(external_atomic_result_printable_uri_tw_css_cjs_namespaceObject);
function _ts_decorate(decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : null === desc ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if ("object" == typeof Reflect && "function" == typeof Reflect.decorate) r = Reflect.decorate(decorators, target, key, desc);
    else for(var i = decorators.length - 1; i >= 0; i--)if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
}
function _ts_metadata(k, v) {
    if ("object" == typeof Reflect && "function" == typeof Reflect.metadata) return Reflect.metadata(k, v);
}
class AtomicResultPrintableUri extends (0, light_dom_cjs_namespaceObject.LightDomMixin)(external_lit_namespaceObject.LitElement) {
    connectedCallback() {
        super.connectedCallback();
        const slotName = 'attributes';
        this.linkAttributes = (0, attributes_slot_cjs_namespaceObject.getAttributesFromLinkSlotContent)(this, slotName);
    }
    disconnectedCallback() {
        super.disconnectedCallback();
        this.cleanupLinkCallbacks();
    }
    initialize() {
        if (!this.result && this.resultController.item) {
            const item = this.resultController.item;
            if ('result' in item) this.result = item.result;
            else this.result = item;
        }
        if (this.result) this.interactiveResult = (0, headless_namespaceObject.buildInteractiveResult)(this.bindings.engine, {
            options: {
                result: this.result
            }
        });
    }
    render() {
        return (0, external_lit_namespaceObject.html)`${(0, when_js_namespaceObject.when)(this.result, ()=>this.renderPrintableUri())}`;
    }
    cleanupLinkCallbacks() {
        this.linkCleanupCallbacks.forEach((cleanup)=>cleanup());
        this.linkCleanupCallbacks = [];
    }
    getIndexOfEllipsis(parentsCount) {
        const valuesToHide = Math.max(2, parentsCount - this.maxNumberOfParts);
        const valuesToShowBeforeEllipsis = parentsCount - valuesToHide - 1;
        return valuesToShowBeforeEllipsis;
    }
    renderEllipsis() {
        return (0, external_lit_namespaceObject.html)`
      <li>
        <button
          aria-label=${this.bindings.i18n.t('collapsed-uri-parts')}
          aria-expanded=${this.listExpanded}
          @click=${(e)=>{
            e.stopPropagation();
            this.listExpanded = true;
            requestAnimationFrame(()=>{
                this.expandedPartElement?.focus();
            });
        }}
        >
          ...
        </button>
        ${this.renderSeparator()}
      </li>
    `;
    }
    get allParents() {
        const parentsXml = (0, utils_cjs_namespaceObject.parseXML)(`${this.result.raw.parents}`);
        const parents = Array.from(parentsXml.getElementsByTagName('parent'));
        const ellipsisIndex = this.getIndexOfEllipsis(parents.length);
        return parents.map((parent, i)=>{
            const name = parent.getAttribute('name');
            const uri = parent.getAttribute('uri');
            return {
                name,
                uri,
                isEllipsisTarget: i === ellipsisIndex,
                isLast: i === parents.length - 1
            };
        });
    }
    renderSeparator() {
        return (0, external_lit_namespaceObject.html)`
      <atomic-icon
        class="result-printable-uri-separator"
        .icon=${arrow_right_cjs_default()}
        role="separator"
      ></atomic-icon>
    `;
    }
    renderParents() {
        const parents = this.allParents;
        if (this.listExpanded || parents.length <= this.maxNumberOfParts) return parents.map((parent)=>this.renderParentListItem(parent));
        const ellipsisIndex = this.getIndexOfEllipsis(parents.length);
        return (0, external_lit_namespaceObject.html)`
      ${parents.slice(0, ellipsisIndex).map((parent)=>this.renderParentListItem(parent))}
      ${this.renderEllipsis()}
      ${parents.slice(-1).map((parent)=>this.renderParentListItem(parent))}
    `;
    }
    renderParentListItem(parent) {
        return (0, external_lit_namespaceObject.html)`
      <li>
        ${this.renderLink(parent.name, parent.uri, parent.isEllipsisTarget)}
        ${parent.isLast ? external_lit_namespaceObject.nothing : this.renderSeparator()}
      </li>
    `;
    }
    renderLink(content, uri, shouldSetTarget) {
        return (0, item_link_cjs_namespaceObject.renderLinkWithItemAnalytics)({
            props: {
                href: uri,
                title: content ?? void 0,
                onSelect: ()=>this.interactiveResult.select(),
                onBeginDelayedSelect: ()=>this.interactiveResult.beginDelayedSelect(),
                onCancelPendingSelect: ()=>this.interactiveResult.cancelPendingSelect(),
                attributes: this.linkAttributes,
                ref: shouldSetTarget ? (el)=>{
                    if (el instanceof HTMLAnchorElement) this.expandedPartElement = el;
                } : void 0,
                onInitializeLink: this.registerLinkCleanup
            }
        })(content ? (0, external_lit_namespaceObject.html)`${content}` : external_lit_namespaceObject.nothing);
    }
    renderPrintableUri() {
        const parents = this.allParents;
        if (parents.length) return (0, external_lit_namespaceObject.html)`
        <ul aria-label=${this.bindings.i18n.t('printable-uri')}>
          ${this.renderParents()}
        </ul>
      `;
        return (0, item_link_cjs_namespaceObject.renderLinkWithItemAnalytics)({
            props: {
                href: this.result.clickUri,
                ariaLabel: this.result.printableUri || this.result.clickUri,
                onSelect: ()=>this.interactiveResult.select(),
                onBeginDelayedSelect: ()=>this.interactiveResult.beginDelayedSelect(),
                onCancelPendingSelect: ()=>this.interactiveResult.cancelPendingSelect(),
                attributes: this.linkAttributes,
                onInitializeLink: this.registerLinkCleanup
            }
        })((0, external_lit_namespaceObject.html)`<atomic-result-text field="printableUri"></atomic-result-text>`);
    }
    constructor(){
        super(), this.maxNumberOfParts = 5, this.listExpanded = false, this.resultController = (0, result_context_controller_cjs_namespaceObject.createResultContextController)(this), this.linkCleanupCallbacks = [], this.registerLinkCleanup = (cleanup)=>{
            this.linkCleanupCallbacks.push(cleanup);
        };
        new validate_props_controller_cjs_namespaceObject.ValidatePropsController(this, ()=>({
                maxNumberOfParts: this.maxNumberOfParts
            }), AtomicResultPrintableUri.propsSchema);
    }
}
AtomicResultPrintableUri.styles = external_atomic_result_printable_uri_tw_css_cjs_default();
AtomicResultPrintableUri.propsSchema = new bueno_namespaceObject.Schema({
    maxNumberOfParts: new bueno_namespaceObject.NumberValue({
        min: 3
    })
});
_ts_decorate([
    (0, decorators_js_namespaceObject.property)({
        type: Number,
        reflect: true,
        attribute: 'max-number-of-parts'
    })
], AtomicResultPrintableUri.prototype, "maxNumberOfParts", void 0);
_ts_decorate([
    (0, decorators_js_namespaceObject.state)(),
    _ts_metadata("design:type", "u" < typeof Bindings ? Object : Bindings)
], AtomicResultPrintableUri.prototype, "bindings", void 0);
_ts_decorate([
    (0, decorators_js_namespaceObject.state)(),
    _ts_metadata("design:type", "u" < typeof Error ? Object : Error)
], AtomicResultPrintableUri.prototype, "error", void 0);
_ts_decorate([
    (0, decorators_js_namespaceObject.state)()
], AtomicResultPrintableUri.prototype, "listExpanded", void 0);
_ts_decorate([
    (0, decorators_js_namespaceObject.state)(),
    _ts_metadata("design:type", "u" < typeof Result ? Object : Result)
], AtomicResultPrintableUri.prototype, "result", void 0);
_ts_decorate([
    (0, binding_guard_cjs_namespaceObject.bindingGuard)(),
    (0, error_guard_cjs_namespaceObject.errorGuard)(),
    _ts_metadata("design:type", Function),
    _ts_metadata("design:paramtypes", []),
    _ts_metadata("design:returntype", void 0)
], AtomicResultPrintableUri.prototype, "render", null);
AtomicResultPrintableUri = _ts_decorate([
    (0, decorators_js_namespaceObject.customElement)('atomic-result-printable-uri'),
    (0, bindings_cjs_namespaceObject.bindings)(),
    _ts_metadata("design:type", Function),
    _ts_metadata("design:paramtypes", [])
], AtomicResultPrintableUri);
exports.AtomicResultPrintableUri = __webpack_exports__.AtomicResultPrintableUri;
for(var __rspack_i in __webpack_exports__)if (-1 === [
    "AtomicResultPrintableUri"
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
Object.defineProperty(exports, '__esModule', {
    value: true
});
