import { r as registerInstance, g as getContext, h, d as getElement } from './core-e0edbf68.js';

const definitions = new Map();
/**
 * Make sure only 1 define call is made for an element.
 * @param name
 * @param def
 * @returns
 */
const define = (name, def) => {
    console.log("define:", name);
    const existing = definitions.get(name);
    console.log("existing:", existing);
    if (existing) {
        if (existing.ready) {
            return;
        }
        if (existing.inProgress) {
            return;
        }
        if (existing.error) {
            throw existing.error;
        }
    }
    definitions.set(name, { inProgress: true });
    try {
        customElements.define(name, def);
    }
    catch (e) {
        /**
         * It can be the case that different tags will use the same CustomElement.
         * We don't want to process all the markup so we wrap the definition in an anonymous class.
         */
        if (e && e.code === DOMException.NOT_SUPPORTED_ERR) {
            try {
                if (customElements.get(name)) {
                    return;
                }
                customElements.define(name, class extends def {
                });
            }
            catch (e) {
                console.error("wrapped class failed");
                definitions.set(name, { inProgress: false, error: e });
            }
        }
        else {
            definitions.set(name, { inProgress: false, error: e });
        }
    }
    customElements
        .whenDefined(name)
        .then(() => {
        definitions.set(name, { inProgress: false, ready: true });
    })
        .catch(e => {
        definitions.set(name, { inProgress: false, error: e });
    });
};
const whenDefined = (name) => {
    return customElements.whenDefined(name);
};

const prepareUrl = (u) => {
    if (u.startsWith("http")) {
        return u;
    }
    if (u.startsWith("/")) {
        return u;
    }
    return `https://cdn.jsdelivr.net/npm/${u}`;
};
function loadScript(src) {
    return new Promise(function (resolve, reject) {
        let s;
        s = document.createElement('script');
        s.type = 'module';
        s.src = src;
        s.onload = resolve;
        s.onerror = reject;
        document.head.appendChild(s);
    });
}
const load = async (tag, url) => {
    const def = customElements.get(tag);
    if (def) {
        console.log("already defined - skip load");
        return whenDefined(tag);
    }
    const fullUrl = prepareUrl(url);
    try {
        await loadScript(fullUrl);
    }
    catch (e) {
        console.error(e.toString());
    }
    return import(fullUrl)
        .then(mod => {
        const Def = mod.definition();
        try {
            define(tag, Def);
        }
        catch (e) {
            return whenDefined(tag);
        }
    })
        .catch(e => {
        console.error("failed to load: ", url);
    });
};

const Embed = class {
    constructor(hostRef) {
        registerInstance(this, hostRef);
        this.doc = getContext(this, "document");
    }
    async componentWillLoad() {
        const dataset = this.el.dataset || {};
        const { tag, url } = dataset;
        if (!url) {
            return;
        }
        if (!tag) {
            return;
        }
        this.tag = tag;
        const names = this.el.getAttributeNames();
        this.params = Object.entries(names).reduce((acc, [index, n]) => {
            if (n !== "data-tag" && n !== "data-url") {
                acc[n] = this.el.getAttribute(n);
            }
            return acc;
        }, {});
        await load(tag, url);
    }
    render() {
        if (this.tag) {
            const Tag = this.tag;
            return h(Tag, Object.assign({}, this.params));
        }
    }
    get el() { return getElement(this); }
    static get style() { return ".pie-loading{position:absolute;height:100%;width:100%;background-color:#fff;bottom:0;left:0;right:0;top:0;z-index:9999;opacity:.4}.pie-loading.fixed{position:fixed}.pie-loading:before{-webkit-box-shadow:0 0 35px rgba(63,81,181,.9);box-shadow:0 0 35px rgba(63,81,181,.9);width:50px;height:50px;-moz-animation:spinPulse 1s infinite ease-in-out;-webkit-animation:spinPulse 1s linear infinite;margin:-25px 0 0 -25px}.pie-loading:after,.pie-loading:before{content:\"\";background-color:transparent;border:5px solid rgba(63,81,181,.9);opacity:.9;border-right:5px solid transparent;border-left:5px solid transparent;border-radius:50px;position:absolute;top:50%;left:50%}.pie-loading:after{-webkit-box-shadow:0 0 15px rgba(63,81,181,.9);box-shadow:0 0 15px rgba(63,81,181,.9);width:30px;height:30px;-moz-animation:spinoffPulse 1s infinite linear;-webkit-animation:spinoffPulse 1s linear infinite;margin:-15px 0 0 -15px}\@-webkit-keyframes spinPulse{0%{-webkit-transform:rotate(160deg);transform:rotate(160deg);opacity:0;-webkit-box-shadow:0 0 1px rgba(63,81,181,.9);box-shadow:0 0 1px rgba(63,81,181,.9)}50%{-webkit-transform:rotate(145deg);transform:rotate(145deg);opacity:1}to{-webkit-transform:rotate(-320deg);transform:rotate(-320deg);opacity:0}}\@keyframes spinPulse{0%{-webkit-transform:rotate(160deg);transform:rotate(160deg);opacity:0;-webkit-box-shadow:0 0 1px rgba(63,81,181,.9);box-shadow:0 0 1px rgba(63,81,181,.9)}50%{-webkit-transform:rotate(145deg);transform:rotate(145deg);opacity:1}to{-webkit-transform:rotate(-320deg);transform:rotate(-320deg);opacity:0}}\@-webkit-keyframes spinoffPulse{0%{-webkit-transform:rotate(0deg);transform:rotate(0deg)}to{-webkit-transform:rotate(1turn);transform:rotate(1turn)}}\@keyframes spinoffPulse{0%{-webkit-transform:rotate(0deg);transform:rotate(0deg)}to{-webkit-transform:rotate(1turn);transform:rotate(1turn)}}.pie-api-player img{max-width:inherit;max-height:inherit}.book-title-k5{text-decoration:underline}.book-title{font-style:italic}.block-quote,.text-block{font-family:serif;margin-left:20%;margin-right:20%;margin-top:5px;margin-bottom:5px;width:60%;border:1px solid #000;padding:2px;display:block}.short-quote{font-weight:700}span.short-quote:before{content:open-quote}span.short-quote:after{content:close-quote}.word-callout{text-decoration:underline}.relative-emphasis{text-transform:uppercase}.content-emphasis{font-weight:700;color:red}span.passage-title:before{content:open-quote}span.passage-title:after{content:close-quote}.proper-name,.variable{font-style:italic}.equation-block{text-align:center;margin-left:20%;margin-right:20%;margin-top:5px;margin-bottom:5px;width:60%;padding:2px;display:block}.embedded-error{font-style:italic}div.passage-subtitle,div.passage-title{font-weight:700;text-align:center}div.passage-author{text-align:center}.p-number{float:left;font-size:.85em}.numbered-paragraph{margin-left:36px}\@media print{.kds-noprint,.noprint{display:none}}.abs,.kds-absolute{border-left:1px solid #000;border-right:1px solid #000;padding:0 .3em 0 .3em}.kds-border-1{border:1px solid #000}.kds-border-2{border:2px solid #000}.center,.kds-center{margin:0;text-align:center}.kds-exponent{font-size:85%}.kds-exponent,sup.frac{margin-left:-.4em;position:relative;top:-1.4em}sup.frac{font-size:70%}.kds-fillin{display:inline-block}.fillin,.kds-fillin{border:1px solid #000;margin:0 .2em;padding-left:1em}.fillin{display:inline-block;*display:inline}.kds-font-larger{font-size:larger}.kds-font-smaller{font-size:smaller}table.kds-fraction{border:0;display:inline-block;font-size:85%;margin:0;padding:0;vertical-align:middle}table.kds-fraction>tbody>tr>td.kds-numerator{border-bottom:1px solid #000;border-left:0;border-right:0;border-top:0;text-align:center}table.kds-fraction>tbody>tr>td.kds-denominator{border:0;text-align:center}table.frac{display:inline-block;*display:inline;vertical-align:middle;font-size:85%;border:0;margin:0;padding:0}.frac .nu{border-left:0;border-top:0;border-right:0;border-bottom:1px solid #000;text-align:center}.frac .de{border:0;text-align:center}.kds-hanging-indent{margin:0;padding-left:3em;text-indent:-3em}.kds-image-caption{font-family:Verdana,Arial,sans-serif;text-align:center}.kds-image-left{float:left;margin-right:5px}.kds-image-right{float:right;margin-left:5px}.indent,.kds-indent{text-indent:3em}.kds-nowrap{display:inline-block;white-space:nowrap}.kds-overline{text-decoration:overline}.kds-parentheses{font-family:Arial Narrow,Arial,sans-serif}.kds-parentheses,b.frac{font-size:300%;font-weight:400;position:relative;top:8px}b.frac{font-family:arial narrow}.kds-pi{font-family:Times New Roman,serif;font-size:1.2em}.newradical{border-collapse:collapse;display:inline-block;*display:inline;vertical-align:middle}.newradical td{border:0;padding:0;vertical-align:top}.newradical td.vinculum{border-top:1px solid #000;padding:0;vertical-align:top}.newradical div.exp{font-size:.7em;position:relative;left:7px;bottom:5px}.kds-underline,.under{text-decoration:underline}.kds-verdana2t,.Verdana2t{border:1px solid #fff;text-align:left;text-decoration:none;vertical-align:top}.kds-whole-number{display:inline-block}.kds-whole-number,.whole{font-size:120%;margin:0 0 0 .2em}.whole{display:inline-block;*display:inline}table.KdsTable01{border:1px solid #000;border-collapse:collapse;border-spacing:0}table.KdsTable01>tbody>tr>th{font-weight:400}table.KdsTable01>tbody>tr>th,table.KdsTable01>tbody>tr>th.bold{background-color:#d3d3d3;border:1px solid #000;padding:2px;text-align:center}table.KdsTable01>tbody>tr>th.bold{font-weight:700}table.KdsTable01>tbody>tr>td,table.KdsTable01>tbody>tr>td.bold{border:1px solid #000;padding:2px;text-align:center}table.KdsTable01>tbody>tr>td.bold{font-weight:700}table.KdsTable02{border:1px solid #000;border-collapse:collapse;border-spacing:0}table.KdsTable02>tbody>tr>th{background-color:#d3d3d3;font-weight:400}table.KdsTable02>tbody>tr>td,table.KdsTable02>tbody>tr>th{border:1px solid #000;padding:2px}.table{width:100%;margin-bottom:1em;background-color:transparent;border-collapse:collapse}.table td,.table th{padding:.75em;vertical-align:top;border-top:1px solid #dee2e6}.table tbody+tbody{border-top:2px solid #dee2e6}.table thead th{vertical-align:bottom;border-bottom:2px solid #dee2e6}th{text-align:inherit}.table-bordered,.table-bordered td,.table-bordered th{border:1px solid #dee2e6}.table-bordered thead td,.table-bordered thead th{border-bottom-width:2px}.table-striped tbody tr:nth-of-type(odd){background-color:rgba(0,0,0,.05)}.text-center{text-align:center!important}.h5,h5{font-size:1.25em}h1,h2,h3,h4,h5,h6{margin-top:0}.h1,.h2,.h3,.h4,.h5,.h6,h1,h2,h3,h4,h5,h6{margin-bottom:.5em;font-family:inherit;font-weight:500;line-height:1.2;color:inherit}.lrn_feature h3{margin-top:0!important}#item,#stimulus{width:50%;float:left}.lrn_width_auto.table{width:auto}.TEX-S3{font-family:MJXZERO,MJXTEX-S3!important}.TEX-S2{font-family:MJXZERO,MJXTEX-S2!important}.TEX-S1{font-family:MJXZERO,MJXTEX-S1!important}.TEX-I{font-family:MJXZERO,MJXTEX-I!important}mjx-c.mjx-c2032{font-family:CerebriSans,serif!important}mjx-c.mjx-c22,mjx-c.mjx-c27{font-family:MJXZERO,serif!important}table{border-collapse:collapse}.evaluate-bottom-border>:first-child:after{content:\"\";display:block;border-bottom:1px solid #d3d3d3}"; }
};

export { Embed as pie_embed };
