class MultipleEntityRow extends Polymer.Element {
    static get template() {
        return Polymer.html`
  
    [[entityName(_config)]]
    
      
        [[entityName(info)]] [[entityState(info)]]
      
      
        
      
    
  
        [[entityName(primary)]]
        [[entityState(primary)]]
       
  
  
      
        [[entityName(secondary)]]
        [[entityState(secondary)]]
       
  
  
    
      [[entityState(_config)]]
    
  
  
    
      
    
  
 `;
    }
    primaryMoreInfo(e) {
        e.stopPropagation();
        this.fireEvent(this._config.primary.entity)
    }
    secondaryMoreInfo(e) {
        e.stopPropagation();
        this.fireEvent(this._config.secondary.entity)
    }
    entityName(data) {
        return data && data.stateObj && data.name !== false ? this.computeStateName(data.stateObj, data.name) : null;
    }
    entityState(data) {
        if (!data || !data.stateObj) return this._hass.localize('state.default.unavailable');
        return data.attribute
            ? data.stateObj.attributes[data.attribute]
                ? `${data.stateObj.attributes[data.attribute]} ${data.unit ? data.unit : ''}`
                : this._hass.localize('state.default.unavailable')
            : this.computeStateValue(data.stateObj, data.unit);
    }
    computeStateName(stateObj, name) {
        return name || (stateObj.attributes.friendly_name === undefined
            ? stateObj.entity_id.substr(stateObj.entity_id.indexOf('.') + 1).replace(/_/g, ' ')
            : stateObj.attributes.friendly_name || '');
    }
    computeStateValue(stateObj, unit) {
        let display;
        const domain = stateObj.entity_id.substr(0, stateObj.entity_id.indexOf("."));
        if (domain === "binary_sensor") {
            if (stateObj.attributes.device_class) {
                display = this._hass.localize(`state.${domain}.${stateObj.attributes.device_class}.${stateObj.state}`);
            }
            if (!display) {
                display = this._hass.localize(`state.${domain}.default.${stateObj.state}`);
            }
        } else if ((unit || stateObj.attributes.unit_of_measurement) && !["unknown", "unavailable"].includes(stateObj.state)) {
            display = `${stateObj.state} ${stateObj.attributes.unit_of_measurement}`;
        } else if (domain === "zwave") {
            display = ["initializing", "dead"].includes(stateObj.state)
                ? this._hass.localize(`state.zwave.query_stage.${stateObj.state}`, 'query_stage', stateObj.attributes.query_stage)
                : this._hass.localize(`state.zwave.default.${stateObj.state}`);
        } else {
            display = this._hass.localize(`state.${domain}.${stateObj.state}`);
        }
        return display ||
            this._hass.localize(`state.default.${stateObj.state}`) ||
            this._hass.localize(`component.${domain}.state.${stateObj.state}`) ||
            stateObj.state;
    }
    setConfig(config) {
        if (!config.entity) throw new Error('Please define an entity.');
        if (config.primary && !config.primary.entity) throw new Error('Please define a primary entity.');
        if (config.secondary && !config.secondary.entity) throw new Error('Please define a secondary entity.');
        this._config = config;
        this.displayToggle = config.toggle === true;
        this.displayValue = !this.displayToggle && !config.hide_state;
        this.displayPrimary = config.primary && config.primary.entity;
        this.displaySecondary = config.secondary && config.secondary.entity;
        this.displayInfo = config.info && config.info.entity;
        this.displayLastChanged = !this.displayInfo && config.secondary_info === 'last-changed';
    }
    set hass(hass) {
        this._hass = hass;
        if (hass && this._config) {
            const stateObj = this._config.entity in hass.states ? hass.states[this._config.entity] : null;
            if (stateObj) {
                this._config.stateObj = stateObj;
                this.primary = Object.assign({}, this._config.primary, {
                    stateObj: this.displayPrimary && this._config.primary.entity in hass.states ?
                        hass.states[this._config.primary.entity] : null
                });
                this.secondary = Object.assign({}, this._config.secondary, {
                    stateObj: this.displaySecondary && this._config.secondary.entity in hass.states ?
                        hass.states[this._config.secondary.entity] : null
                });
                this.info = Object.assign({}, this._config.info, {
                    stateObj: this.displayInfo && this._config.info.entity in hass.states ?
                        hass.states[this._config.info.entity] : null
                });
            }
        }
    }
    fireEvent(entity, options = {}) {
        const event = new Event('hass-more-info', {
            bubbles: options.bubbles || true,
            cancelable: options.cancelable || true,
            composed: options.composed || true,
        });
        event.detail = {entityId: entity};
        this.shadowRoot.dispatchEvent(event);
        return event;
    }
}
customElements.define('multiple-entity-row', MultipleEntityRow);