import { css, html, LitElement } from 'lit';

/******************************************************************************
Copyright (c) Microsoft Corporation.

Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
***************************************************************************** */

function __decorate(decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") 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;
}

/**
 * @license
 * Copyright 2017 Google LLC
 * SPDX-License-Identifier: BSD-3-Clause
 */
const e=e=>n=>"function"==typeof n?((e,n)=>(customElements.define(e,n),n))(e,n):((e,n)=>{const{kind:t,elements:s}=n;return {kind:t,elements:s,finisher(n){customElements.define(e,n);}}})(e,n);

/**
 * @license
 * Copyright 2021 Google LLC
 * SPDX-License-Identifier: BSD-3-Clause
 */var n;null!=(null===(n=window.HTMLSlotElement)||void 0===n?void 0:n.prototype.assignedElements)?(o,n)=>o.assignedElements(n):(o,n)=>o.assignedNodes(n).filter((o=>o.nodeType===Node.ELEMENT_NODE));

function init () {
    var createReloadHandler = function () {
        var iwcClient = window._iwc_instance_;
        var intent_listener = [];
        if (iwcClient && iwcClient.onIntent != null) {
            var previous_iwc_onIntent = iwcClient.onIntent;
            iwcClient.onIntent = function (message) {
                if (message.action === "RELOAD") {
                    console.log(" K!!!!!!!!!!!!!!!!!!!!!!!!!!!! RELOAD!!!!!!!!!!!!!!!!!!!!!");
                    window.location.reload();
                }
                else {
                    for (var i = 0; i < intent_listener.length; i++) {
                        intent_listener[i].apply(this, arguments);
                    }
                }
                previous_iwc_onIntent.apply(this, arguments);
            };
            window._addIwcIntentListener = function (f) {
                intent_listener.push(f);
            };
            window._reloadThisFuckingInstance = function () {
                console.log("Reloading Everything");
                var message = {
                    action: "RELOAD",
                    component: "",
                    data: "",
                    dataType: "",
                    flags: ["PUBLISH_GLOBAL"],
                    extras: {
                        reload: true,
                    },
                };
                iwcClient.publish(message);
            };
        }
        else {
            setTimeout(createReloadHandler, 5000);
        }
    };
    setTimeout(createReloadHandler, 10000);
}

const SyncMetaWidget = (superClass, widgetName) => {
    if (!widgetName) {
        throw new Error("widgetName cannot be empty");
    }
    class SyncMetaWidgetElement extends superClass {
        constructor() {
            super(...arguments);
            this.widgetName = widgetName;
        }
        createRenderRoot() {
            return this;
        }
        render() {
            return html ` <error-alert></error-alert> `;
        }
        firstUpdated() {
            this.hideErrorAlert();
        }
        connectedCallback() {
            super.connectedCallback();
        }
        disconnectedCallback() {
            super.disconnectedCallback();
        }
        hideErrorAlert() {
            $(this.widgetName).find("#alert-message").text("");
            $(this.widgetName).find("error-alert").hide();
        }
        showErrorAlert(message) {
            $(this.widgetName).find("#alert-message").text(message);
            $(this.widgetName).find("error-alert").hide();
        }
    }
    SyncMetaWidgetElement.styles = css `
      .loading {
        position: absolute;
        top: 0;
        right: 0;
        bottom: 0;
        left: 0;
        /*noinspection CssUnknownTarget*/
        background: white url("/img/loading.gif") no-repeat center center;
        z-index: 32032;
        opacity: 0.75;
      }

      #oauthPersonalize,
      #oauthPersonalizeDone,
      #oauthPersonalizeComplete {
        position: relative;
        z-index: 32033;
      }

      #q {
        position: absolute;
        width: 100%;
        height: 3px;
        bottom: 0;
        left: 0;
        cursor: s-resize;
      }
    `;
    return SyncMetaWidgetElement;
};

const CONFIG = {
  TEST: {
    USER: "Luigi Test",
    EMAIL: "luigi.test05@gmail.com",
    CANVAS: false,
    ATTRIBUTE: false,
    PALETTE: false,
    ACTIVITY: false,
  },
  LAYER: {
    META: "META",
    MODEL: "MODEL",
  },
  WIDGET: {
    NAME: {
      MAIN: "Canvas",
      PALETTE: "Palette",
      ATTRIBUTE: "Property Browser",
      ACTIVITY: "User Activity",
      GUIDANCE: "Guidance",
      HEATMAP: "Heatmap",
      METADATA: "METADATA",
      OPENAPI: "Metadata Widget",
      DEBUG: "Debug",
      IMSLD_EXPORT: "IMSLD Export",
      JSON_EXPORT: "JSON Export",
      VIEWCONTROL: "View Control",
    },
  },
  ENTITY: {
    NODE: "node",
    EDGE: "edge",
    ATTR: "attr",
    VAL: "val",
  },
  IWC: {
    FLAG: {
      PUBLISH_GLOBAL: "PUBLISH_GLOBAL",
      PUBLISH_LOCAL: "PUBLISH_LOCAL",
    },
    ACTION: {
      SYNC: "ACTION_SYNC",
      DATA: "ACTION_DATA",
      DATA_ARRAY: "ACTION_DATA_ARRAY",
    },
    POSITION: {
      NODE: {
        ADD: 0,
        DEL: 0,
        POS: 1,
        Z: 2,
        DIM: 3,
      },
      EDGE: {
        ADD: 0,
        DEL: 0,
        MOV: 1,
      },
      ATTR: {
        ADD: 0,
        DEL: 0,
      },
    },
  },
  OPERATION: {
    TYPE: {
      INSERT: "insert",
      UPDATE: "update",
      DELETE: "delete",
    },
  },
  ACTIVITY: {
    TYPE: {
      NODEADD: 0,
      EDGEADD: 1,
      NODEDEL: 2,
      EDGEDEL: 3,
      NODEATTRCHANGE: 4,
    },
  },
  DATA: {
    RELATION: {
      GLOBAL: {
        MAIN: {
          MAIN: {
            OPERATION: "MAIN2MAIN4OPERATION",
          },
        },
      },
      LOCAL: {
        PALETTE: {
          MAIN: {
            TOOLSELECTION: "PALETTE2MAIN4TOOLSELECTION",
          },
        },
        MAIN: {
          ATTRIBUTE: {
            NODESELECTION: "MAIN2ATTRIBUTE4NODESELECTION",
            NODEADDITION: "MAIN2ATTRIBUTE4NODEADDITION",
            ATTRIBUTECHANGE: "MAIN2ATTRIBUTE4ATTRIBUTECHANGE",
          },
          ACTIVITY: {
            NEWACTIVITY: "MAIN2ACTIVITY4NEWACTIVITY",
          },
          PALETTE: {
            TOOLSELECTION: "MAIN2PALETTE4TOOLSELECTION",
          },
        },
        ATTRIBUTE: {
          MAIN: {
            ATTRIBUTECHANGE: "ATTRIBUTE2MAIN4ATTRIBUTECHANGE",
          },
        },
      },
    },
  },
  NS: {
    PERSON: {
      TITLE: "http://purl.org/dc/terms/title",
      JABBERID: "http://xmlns.com/foaf/0.1/jabberID",
      MBOX: "http://xmlns.com/foaf/0.1/mbox",
    },
    MY: {
      MODEL: "my:ns:model",
      METAMODEL: "my:ns:metamodel",
      INSTANCE: "my:ns:instance",
      VIEWPOINT: "my:ns:viewpoint",
      VIEW: "my:ns:view",
      COPY: "my:ns:copy",
      GUIDANCEMODEL: "my:ns:guidancemodel",
      METAMODELPREVIEW: "my:ns:metamodelpreview",
      GUIDANCEMETAMODEL: "my:ns:guidancemetamodel",
      LOGICALGUIDANCEREPRESENTATION: "my:ns:logicalguidancerepresentation",
    },
  },
};

 /**
  * Gets the html tag name of a widget. 
  * Use this function to get the tag name of a or your own widget.
  * @example getWidgetTagName("My Widget") // returns "my-widget-widget"
  * @param {*} name name of the widget
  * @returns  {string} tag name of the widget
  */
function getWidgetTagName(name) {
  if (!name) return;
  if (
    !Object.values(CONFIG.WIDGET.NAME).some(
      (n) => n.toLocaleLowerCase() === name.toLocaleLowerCase()
    )
  ) {
    console.warn(
      `Widget name ${name} is not defined in config.js. Add it to the CONFIG.WIDGET.NAME object.`
    );
  }
  let widgetName = name;
  widgetName = widgetName.replace(/\s+/g, "-");
  return `${widgetName}-widget`.toLowerCase();
}

let IMSLDExportWidget = class IMSLDExportWidget extends SyncMetaWidget(LitElement, getWidgetTagName(CONFIG.WIDGET.NAME.IMSLD_EXPORT)) {
    render() {
        return html `
      <div class="seperating_box">
        <h5>Download IMSLD</h5>
        <button id="imsld">Download ZIP</button>
      </div>
      <div style="font-size:3pt">&nbsp;</div>
      <div class="seperating_box">
        <h5>Integrated Learning Design Environment (ILDE)</h5>
        <form id="ilde_login_form" class="hide">
          <div>
            Please provide your ILDE credentials to use this feature. There are
            different installations of ILDE. Please provide the URL of
            installation that you want to use.
          </div>
          <table>
            <tr>
              <td>Installation:</td>
              <td>
                <input
                  id="ildeResource"
                  name="resource"
                  type="text"
                  size="29"
                  value="http://ilde.upf.edu/"
                />
              </td>
            </tr>
            <tr>
              <td>Username:</td>
              <td><input name="username" type="text" size="29" /></td>
            </tr>
            <tr>
              <td>Password:</td>
              <td><input name="password" type="password" size="29" /></td>
            </tr>
          </table>
          <input type="submit" value="Login" />
          <span class="error_notification hide" style="color:red"> </span>
        </form>
        <div id="ilde_upload_form">
          <div id="createIldeDiv" class="">
            <div>
              This learning design can be synchronized with ILDE. If you want to
              synchronize it with an existing ILDE design, please provide the
              URL below. Otherwise just click 'Start ILDE Sync!'
            </div>
            <br />
            <div>
              URL of existing design (optional):
              <input id="existingIldeUrl" type="text" value="" size="40" />
            </div>
            <br />
            <input
              id="createIldeButton"
              type="submit"
              value="Start ILDE Sync!"
            />
          </div>
          <div id="syncIldeDiv" class="hide">
            <div>
              This design is now available also on ILDE:
              <a id="ildeLink" href="" target="_blank"></a>
            </div>
            <br />
            <div>Click to push your changes:</div>
            <input id="syncIldeButton" type="submit" value="Push to ILDE" />
            <input
              id="removeIldeButton"
              type="submit"
              value="Unlink from ILDE"
            />
            <span id="success_notification" class="hide" style="color:green">
              Success!
            </span>
          </div>
          <span class="error_notification hide" style="color:red"> </span>
        </div>
      </div>
    `;
    }
    connectedCallback() {
        super.connectedCallback();
        init();
    }
    disconnectedCallback() {
        super.disconnectedCallback();
    }
};
IMSLDExportWidget.styles = css `
    .loading_button {
      background-image: url('<%= grunt.config("baseUrl") %>/img/loading_small.gif');
      background-repeat: no-repeat;
      background-position: right center;
      padding-right: 20px;
    }
    .hide {
      display: none;
    }

    #ilde_login_form * {
      font-size: 12px;
    }
    #ilde_upload_form * {
      font-size: 12px;
    }
    .seperating_box {
      border: 1px solid;
      border-radius: 7px;
      margin: 18px 20px 7px 7px;
      padding: 7px 20px 7px 7px;
      position: relative;
    }
    .seperating_box > h5 {
      font-weight: normal;
      font-style: italic;
      position: absolute;
      top: -40px;
      left: 4px;
    }
  `;
IMSLDExportWidget = __decorate([
    e(getWidgetTagName(CONFIG.WIDGET.NAME.IMSLD_EXPORT))
], IMSLDExportWidget);

export { IMSLDExportWidget };
//# sourceMappingURL=imsld.export.widget.js.map
