text: define(["meta", "require", "../../../lit-element/lit-element.js", "../../simple-icon/simple-icon.js", "../../simple-icon/lib/simple-icons.js", "../../simple-icon/lib/simple-icon-button.js", "../../hax-iconset/lib/simple-hax-iconset.js", "../../hax-body-behaviors/lib/HAXFields.js", "../../simple-fields/simple-fields.js", "../../simple-fields/lib/simple-fields-form.js", "./hax-element-card-list.js"], function (meta, _require, _litElement, _simpleIcon, _simpleIcons, _simpleIconButton, _simpleHaxIconset, _HAXFields, _simpleFields, _simpleFieldsForm, _haxElementCardList) {
    "use strict";
  
    meta = babelHelpers.interopRequireWildcard(meta);
    _require = babelHelpers.interopRequireWildcard(_require);
  
    var _templateObject_b6b54c10af6311ebbe560fe9c4727db1, _templateObject2_b6b54c10af6311ebbe560fe9c4727db1;
  
    function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }
  
    function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { babelHelpers.defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
  
    function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = babelHelpers.getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = babelHelpers.getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return babelHelpers.possibleConstructorReturn(this, result); }; }
  
    function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }
  
    /**
     * `hax-element-list-selector`
     * selects a hax element
     * @demo demo/hax.html
     * @element hax-element-list-selector
     */
    var HaxElementListSelector = /*#__PURE__*/function (_LitElement) {
      babelHelpers.inherits(HaxElementListSelector, _LitElement);
  
      var _super = _createSuper(HaxElementListSelector);
  
      function HaxElementListSelector() {
        var _this;
  
        babelHelpers.classCallCheck(this, HaxElementListSelector);
        _this = _super.call(this);
        _this.loading = false;
        _this.cols = 3;
        _this.showCardList = false;
        _this.imports = [];
        _this.haxData = [];
        _this.noSchema = {};
        _this.method = "GET";
        _this.autoload = true;
        _this.HaxSchematizer = _HAXFields.HaxSchematizer;
        _this.HaxElementizer = _HAXFields.HaxElementizer; // default fields json blob, most implementations should provide their own though obviously
  
        _this.fieldsEndpoint = _this.pathFromUrl(decodeURIComponent(meta.url)) + "fields.json"; // allow flobal base path focibly set
  
        if (window.WCGlobalBasePath) {
          _this.basePath = window.WCGlobalBasePath;
        } else {
          _this.basePath = _this.pathFromUrl(decodeURIComponent(meta.url)) + "../../../";
        } // allow global definition of wc-registry for custom ones
  
  
        if (window.WCGlobalRegistryFileName) {
          _this.__regFile = window.WCGlobalRegistryFileName;
        } else {
          _this.__regFile = "wc-registry.json";
        }
  
        setTimeout(function () {
          window.addEventListener("a11y-tabs-active-changed", _this._activeTabChanged.bind(babelHelpers.assertThisInitialized(_this)));
        }, 0);
  
        _this.addEventListener("response", _this._response);
  
        _this.addEventListener("value-changed", _this._valueChanged);
  
        return _this;
      }
  
      babelHelpers.createClass(HaxElementListSelector, [{
        key: "pathFromUrl",
        value: // simple path from a url modifier
        function pathFromUrl(url) {
          return url.substring(0, url.lastIndexOf("/") + 1);
        }
      }, {
        key: "render",
        value: function render() {
          var _this2 = this;
  
          return (0, _litElement.html)(_templateObject_b6b54c10af6311ebbe560fe9c4727db1 || (_templateObject_b6b54c10af6311ebbe560fe9c4727db1 = babelHelpers.taggedTemplateLiteral(["\n      <simple-fields-form\n        id=\"form\"\n        autoload\n        load-endpoint=\"", "\"\n        method=\"", "\"\n        .schematizer=\"", "\"\n        .elementizer=\"", "\"\n        @response=\"", "\"\n        @haxcore.search.haxcore-search-columns-value-changed=\"", "\"\n        @value-changed=\"", "\"\n      >\n      </simple-fields-form>\n    "])), this.fieldsEndpoint, this.method, this.HaxSchematizer, this.HaxElementizer, this._response, function (e) {
            return _this2.cols = e.detail.value;
          }, this._valueChanged);
        }
      }, {
        key: "_activeTabChanged",
        value: function _activeTabChanged(e) {
          if (e.detail.activeTab == "haxcore.search") {
            this.showCardList = true;
          } else {
            this.showCardList = false;
          }
        }
      }, {
        key: "_getElement",
        value: function _getElement(id) {
          return this.form && this.form.formElements && this.form.formElements[id] ? this.form.formElements[id].element : undefined;
        }
      }, {
        key: "form",
        get: function get() {
          return this.shadowRoot && this.shadowRoot.querySelector("#form") ? this.shadowRoot.querySelector("#form") : undefined;
        }
      }, {
        key: "cardList",
        get: function get() {
          return this._getElement("haxcore.search.haxcore-search-autoloader");
        }
      }, {
        key: "searchColumns",
        get: function get() {
          this._getElement("haxcore.search.haxcore-search-columns");
        }
      }, {
        key: "haxTags",
        get: function get() {
          this._getElement("haxcore.providerdetails.haxcore-providerdetails-haxtags");
        }
      }, {
        key: "otherTags",
        get: function get() {
          this._getElement("haxcore.providerdetails.haxcore-providerdetails-othertags");
        }
      }, {
        key: "updated",
        value: function updated(changedProperties) {
          var _this3 = this;
  
          changedProperties.forEach( /*#__PURE__*/function () {
            var _ref = babelHelpers.asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee2(oldValue, propName) {
              var tag, renderHaxData, i, search;
              return regeneratorRuntime.wrap(function _callee2$(_context3) {
                while (1) {
                  switch (_context3.prev = _context3.next) {
                    case 0:
                      if (propName == "loading" && _this3.cardList) _this3.cardList.loading = _this3.loading;
                      if (propName == "showCardList" && _this3.cardList) _this3.cardList.showCardList = _this3.showCardList;
                      if (propName == "cols" && _this3.cardList) _this3.cardList.cols = _this3.cols;
                      if (propName == "cols" && _this3.searchColumns) _this3.searchColumns.value = _this3.cols;
  
                      if (propName == "wcRegistryEndpoint") {
                        _this3.haxData = [];
                        _this3.imports = [];
                        fetch(_this3.wcRegistryEndpoint).then(function (response) {
                          _this3.loading = true;
                          return response.json();
                        }).then(function (data) {
                          _this3.imports = data;
                        });
                      } // when imports changes make sure we import everything found
  
  
                      if (!(propName == "imports")) {
                        _context3.next = 7;
                        break;
                      }
  
                      return _context3.delegateYield( /*#__PURE__*/regeneratorRuntime.mark(function _callee() {
                        var list, noSchema, _loop;
  
                        return regeneratorRuntime.wrap(function _callee$(_context2) {
                          while (1) {
                            switch (_context2.prev = _context2.next) {
                              case 0:
                                list = _this3.haxData, noSchema = _this3.noSchema;
                                _loop = /*#__PURE__*/regeneratorRuntime.mark(function _loop() {
                                  var file;
                                  return regeneratorRuntime.wrap(function _loop$(_context) {
                                    while (1) {
                                      switch (_context.prev = _context.next) {
                                        case 0:
                                          file = _this3[propName][tag];
                                          _context.prev = 1;
                                          _context.next = 4;
                                          return new Promise(function (res, rej) {
                                            return _require.default(["".concat(_this3.basePath).concat(file)], res, rej);
                                          }).then(function (module) {
                                            if (module && Object.keys(module) && Object.keys(module)[0] && module[Object.keys(module)[0]] && module[Object.keys(module)[0]].haxProperties && module[Object.keys(module)[0]].haxProperties.gizmo && module[Object.keys(module)[0]].haxProperties.gizmo.title) {
                                              var detail = {
                                                tag: tag,
                                                file: file,
                                                showDemo: false,
                                                schema: module[Object.keys(module)[0]].haxProperties
                                              };
                                              list.push(detail);
                                              _this3.haxData = babelHelpers.toConsumableArray(list);
                                            } else {
                                              noSchema[tag] = file; //console.log(`${tag} doesn't have haxSchema`);
                                            }
                                          });
  
                                        case 4:
                                          _context.next = 9;
                                          break;
  
                                        case 6:
                                          _context.prev = 6;
                                          _context.t0 = _context["catch"](1);
                                          console.warn(_context.t0);
  
                                        case 9:
                                        case "end":
                                          return _context.stop();
                                      }
                                    }
                                  }, _loop, null, [[1, 6]]);
                                });
                                _context2.t0 = regeneratorRuntime.keys(_this3[propName]);
  
                              case 3:
                                if ((_context2.t1 = _context2.t0()).done) {
                                  _context2.next = 8;
                                  break;
                                }
  
                                tag = _context2.t1.value;
                                return _context2.delegateYield(_loop(), "t2", 6);
  
                              case 6:
                                _context2.next = 3;
                                break;
  
                              case 8:
                                _this3.noSchema = {};
                                _this3.noSchema = noSchema;
                                _this3.loading = false;
  
                              case 11:
                              case "end":
                                return _context2.stop();
                            }
                          }
                        }, _callee);
                      })(), "t0", 7);
  
                    case 7:
                      // this is the local data we don't let change
                      if (propName == "haxData") {
                        _this3.filteredHaxData = babelHelpers.toConsumableArray(_this3.haxData);
  
                        if (_this3.haxData.length > 0) {
                          renderHaxData = {};
  
                          for (i in _this3.haxData) {
                            renderHaxData[_this3.haxData[i].tag] = _this3.haxData[i].file;
                          }
  
                          if (_this3.cardList) {
                            search = _this3.form && _this3.form.value && _this3.form.value.haxcore && _this3.form.value.haxcore.search ? _this3.form.value.haxcore.search : undefined;
                            _this3.cardList.list = _this3.filteredHaxData;
                            if (search) _this3.applyFilters(search);
  
                            _this3.cardList.requestUpdate();
                          }
  
                          if (_this3.haxTags) _this3.haxTags.editorValue = JSON.stringify(renderHaxData, null, 2);
                        }
                      }
  
                      if (propName == "noSchema" && Object.keys(_this3.noSchema).length > 0 && _this3.otherTags) _this3.otherTags.editorValue = JSON.stringify(_this3.noSchema, null, 2);
  
                    case 9:
                    case "end":
                      return _context3.stop();
                  }
                }
              }, _callee2);
            }));
  
            return function (_x, _x2) {
              return _ref.apply(this, arguments);
            };
          }());
        }
      }, {
        key: "applyFilters",
        value: function applyFilters(filters) {
          if (this.cardList) {
            var data = babelHelpers.toConsumableArray(this.haxData);
            Object.keys(filters || {}).forEach(function (key) {
              if (filters[key] != "") {
                switch (key) {
                  case "haxcore-search-search":
                    data = data.filter(function (item) {
                      if (item.schema.gizmo.title.toLowerCase().includes(filters[key].toLowerCase())) {
                        return true;
                      }
  
                      return false;
                    });
                    break;
  
                  case "haxcore-search-tags":
                    data = data.filter(function (item) {
                      if (item.schema.gizmo.groups.includes(filters[key])) {
                        return true;
                      }
  
                      return false;
                    });
                    break;
  
                  case "haxcore-search-hasdemo":
                    // only filter if box checked otherwise show all
                    if (filters[key]) {
                      data = data.filter(function (item) {
                        if (item.schema.demoSchema) {
                          return true;
                        }
  
                        return false;
                      });
                    }
  
                    break;
                }
              }
            });
            this.cardList.filteredTags = babelHelpers.toConsumableArray(data).map(function (item) {
              return item.tag;
            });
            this.cardList.requestUpdate();
          }
        }
        /**
         * Listen for response and then apply initial settings
         */
  
      }, {
        key: "_response",
        value: function _response(e) {
          if (this.searchColumns) this.searchColumns.value = this.cols;
          if (this.cardList) this.cardList.cols = this.cols; //this._valueChanged(e);
        }
        /**
         * notice any value changing and then getting the form fresh
         */
  
      }, {
        key: "_valueChanged",
        value: function _valueChanged() {
          var _this4 = this;
  
          clearTimeout(this.__valueDebounce);
          this.__valueDebounce = setTimeout(function () {
            var haxcore = _this4.form && _this4.form.value && _this4.form.value.haxcore ? _this4.form.value.haxcore : undefined;
  
            if (haxcore) {
              var cols = haxcore.search && haxcore.search["haxcore-search-columns"] ? haxcore.search["haxcore-search-columns"] : undefined; //set columns
  
              if (cols) _this4.cols = cols; // look for CDN provider
  
              if (haxcore.providers["haxcore-providers-cdn"] == "other") {
                _this4.wcRegistryEndpoint = haxcore.providers["haxcore-providers-other"] + _this4.__regFile;
              } else {
                _this4.wcRegistryEndpoint = haxcore.providers["haxcore-providers-cdn"] + _this4.__regFile;
              } // apply filters
  
  
              _this4.applyFilters(haxcore.search);
  
              if (_this4.cardList) {
                _this4.dispatchEvent(new CustomEvent("appstore-changed", {
                  detail: {
                    value: _this4.getAppstoreValues()
                  }
                }));
              }
            }
          }, 50);
        }
        /**
         * Return the appstore values
         */
  
      }, {
        key: "getAppstoreValues",
        value: function getAppstoreValues() {
          // get form values
          var value = this.shadowRoot.querySelector("#form").submit();
          value.haxcore = value.haxcore || {
            templates: {},
            providers: {}
          };
          var appstore = {
            providers: {
              cdn: value.haxcore.providers["haxcore-providers-cdn"],
              other: value.haxcore.providers["haxcore-providers-other"],
              pk: value.haxcore.providers["haxcore-providers-pk"]
            },
            apps: {},
            stax: value.haxcore.templates["haxcore-templates-layouts"],
            autoloader: value.haxcore.search["haxcore-search-autoloader"]
          }; // find the API keys
  
          for (var key in value.haxcore.integrations) {
            appstore.apps[key.replace("haxcore-integrations-", "")] = value.haxcore.integrations[key];
          }
  
          return appstore;
        }
        /**
         * Autoloader is a simple keypair
         */
  
      }, {
        key: "getAutoloader",
        value: function getAutoloader(data) {
          var autoload = {};
  
          for (var i in data) {
            if (data[i].status) {
              autoload[data[i].tag] = data[i].file;
            }
          }
  
          return autoload;
        }
      }], [{
        key: "tag",
        get: function get() {
          return "hax-element-list-selector";
        }
      }, {
        key: "styles",
        get: function get() {
          return [(0, _litElement.css)(_templateObject2_b6b54c10af6311ebbe560fe9c4727db1 || (_templateObject2_b6b54c10af6311ebbe560fe9c4727db1 = babelHelpers.taggedTemplateLiteral(["\n        :host {\n          display: block;\n        }\n      "])))];
        }
      }, {
        key: "properties",
        get: function get() {
          return _objectSpread(_objectSpread({}, _simpleFieldsForm.SimpleFieldsForm.properties), {}, {
            HaxSchematizer: {
              type: Object
            },
            HaxElementizer: {
              type: Object
            },
  
            /**
             * Show card list so that it SEEMS like its happenign when we click HAX elements
             */
            showCardList: {
              type: Boolean
            },
  
            /**
             * JS imports
             */
            imports: {
              type: Object
            },
  
            /**
             * HAXSchema array
             */
            haxData: {
              type: Array
            },
  
            /**
             * Valid tags on the CDN but that don't have haxSchema.
             */
            noSchema: {
              type: Object
            },
  
            /**
             * Data filtered by form changes
             */
            filteredHaxData: {
              type: Array
            },
  
            /**
             * Columns to render
             */
            cols: {
              type: Number
            },
  
            /**
             * End point to load this data
             */
            fieldsEndpoint: {
              type: String,
              attribute: "fields-endpoint"
            },
  
            /**
             * End point to load up a list of imports
             */
            wcRegistryEndpoint: {
              type: String,
              attribute: "wc-registry-endpoint"
            },
  
            /**
             * Request method
             */
            method: {
              type: String
            },
            loading: {
              type: Boolean,
              reflect: true
            }
          });
        }
      }]);
      return HaxElementListSelector;
    }(_litElement.LitElement);
  
    window.customElements.define(HaxElementListSelector.tag, HaxElementListSelector);
  });